Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在课堂上使用带self的decorator_Python_Bottle - Fatal编程技术网

Python 在课堂上使用带self的decorator

Python 在课堂上使用带self的decorator,python,bottle,Python,Bottle,我是一名学生,我想用一个带有瓶子微框架的小网站来练习MVC和OOP 因此,我的控制器实例化一个瓶子对象,并将其发送到我的模型。我的模型需要使用瓶类的“路由”装饰器来定义路由(例如,@app.route(“/blog”)) 但是看起来我不能在类中使用decorator,因为self不存在于方法之外 那么,我如何在MVC和OOP方法中做到这一点呢?ie我希望避免在类外实例化瓶子并将其用作全局变量 谢谢 #!/usr/bin/env python #-*-coding:utf8-*- from bo

我是一名学生,我想用一个带有瓶子微框架的小网站来练习MVC和OOP

因此,我的控制器实例化一个瓶子对象,并将其发送到我的模型。我的模型需要使用瓶类的“路由”装饰器来定义路由(例如,
@app.route(“/blog”)

但是看起来我不能在类中使用decorator,因为
self
不存在于方法之外

那么,我如何在MVC和OOP方法中做到这一点呢?ie我希望避免在类外实例化瓶子并将其用作全局变量

谢谢

#!/usr/bin/env python
#-*-coding:utf8-*-

from bottle import Bottle, run




class Model():
    def __init__(self, app):
        self.app = app


    @self.app.route("/hello") ### dont work because self dont exist here
    def hello(self):
        return "hello world!"


class View():
    pass


class Controller():
    def __init__(self):
        self.app = Bottle()
        self.model=Model(self.app)



if __name__ == "__main__": 
    run(host="localhost", port="8080", debug=True)
单向:

class Model(object):
    def __init__(self, app):
        self.app = app
        self.hello = self.app.route("/hello")(self.hello) 

    def hello(self):
        return "hello world!"