【发布时间】:2019-05-14 10:12:44
【问题描述】:
我希望这些函数成为我正在构建的类的一部分,但我收到一个错误装饰器函数可能是部门中的一个函数的问题。有解决问题的方法吗?谢谢。
import engineio
class Websocket:
def __init__(self):
self.eio = engineio.Client()
self.eio.connect('http://localhost:5000')
self.eio.wait()
# get error in this function
@eio.on('connect')
def on_connect():
print('connection established')
【问题讨论】:
-
eio在这种情况下指的是什么?定义函数时会应用装饰器(因此当 Python 找到def ...语句并执行它时)。在类体内,这意味着还没有类对象,更不用说该类的实例了。 -
另外,
self.eio.wait()blocks,你不想在类的__init__方法中使用它。