Python 如何将self传递给瓶子路由功能?

Python 如何将self传递给瓶子路由功能?,python,scope,bottle,Python,Scope,Bottle,我作为环境的一部分运行,无法理解如何将变量传递给其路由函数。以下代码运行良好: import bottle class WebServer(): message1 = 'hello message1' def __init__(self): self.message2 = 'hello message2' bottle.run(host='localhost', port=8080) @bottle.get('/hello')

我作为环境的一部分运行,无法理解如何将变量传递给其路由函数。以下代码运行良好:

import bottle

class WebServer():
    message1 = 'hello message1'

    def __init__(self):
        self.message2 = 'hello message2'
        bottle.run(host='localhost', port=8080)

    @bottle.get('/hello')
    def hello():
        # here I would like to return message1 or message2
        return 'unfortunately only a static message so far'

WebServer()
在调用
/hello
URL时,我想返回
消息1
消息2
(两种不同的情况)。不过,我不知道如何将
self
传递给
hello()
。我该怎么做呢?

继的评论之后,我将代码改写为

import bottle

class WebServer():
    message1 = 'hello message1'

    def __init__(self):
        self.message2 = 'hello message2'
        bottle.run(host='localhost', port=8080)

    def hello(self):
        # here I would like to return message1 or message2
        # return 'unfortunately only a static message so far'
        # now it works
        return self.message1  # or self.message2

w = WebServer()
bottle.route('/hello', 'GET', w.hello)
这最终成为一种更干净的路由结构(IMHO)

不幸的是,这似乎不适用于错误路由。我没有找到一种方法来扭转局面

@bottle.error(404)
def error404(error):
    print('error 404')
    return

类似于上面的(
bottle.error
..)提到这是一个装饰器(所以我想它必须保持原样)

不知道bottle,但将self作为第一个参数传递。def你好(self):@ndpu:谢谢!使用这些信息和文档,我得到了一个更干净、更有效的代码。