在课堂上使用装饰师与自我

时间:2017-10-29 02:50:37

标签: python bottle

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

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

但看起来我不能在类中使用装饰器,因为self不存在于方法之外。

那么我怎样才能在MVC和OOP方法中做到这一点?即我想避免在类之外实例化Bottle并将其用作全局变量。

感谢。

#!/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)

1 个答案:

答案 0 :(得分:1)

一种方式:

<Style/>
相关问题