首先,创建我自己的类对象并继承api
class mainobject1(XAPI):
def __init__(self):
XAPI.__init__(self)
self.count = 0
# built-in to run api
self.run()
# default api method name
def error(self, regId, errorCode, errorString):
print("Error:", errorCode)
self.count += 1
这允许我访问“error”方法中的self.count,但是否可以将API方法“error”指向其他特定名称,如“xapi_error”?我的程序中几乎没有api,所以我想清楚地声明它们。 我只是不想在mainobject类中访问self.error
因此,单独创建一个api对象
class myapi(XAPI):
def __init__(self):
XAPI.__init__(self)
# default api method name
def error(self, regId, errorCode, errorString):
print("Error:", errorCode)
# self.count += 1 # not working here
class mainobject2():
def __init__(self):
self.api = myapi()
self.count = 0
# built-in to run api
self.api.run()
这种方法不会有命名问题,但不会直接访问我自己的类变量self.count
然后我尝试了以下方法:
......
class mainobject2():
......
# Failed approach
@self.api.error # <------ ERROR
def xapi_error(self, regId, errorCode, errorString):
......
NameError:未定义名称“self”
很久以前,我在这个@方法上取得了一些成功,但是忘记了它是什么以及如何使用它
......
import ctypes
class mainobject2():
# Failed approach
def RegCallback(self):
def xapi_error(None, regId, errorCode, errorString):
......
self.xapi_ErrorAddr = PYFUNCTYPE(None, c_char_p)(xapi_error)
self.app.error(self.xapi_ErrorAddr) # <------ ERROR
TypeError:error()缺少2个必需的位置参数:“errorCode”和“errorString”
我能够使用ctypes将dll回调映射到我的类…我想知道python回调是否有类似的东西
最后,我的结构:
mainobject(self)
├── objectx (self)
│ └── xapi
├── objecty (self)
│ └── yapi
......
self参数彼此共享,因此可以在任何地方访问。所以我希望,在线程代码中,它将是:
import XAPI
......
class objectx():
def __init__(self):
self.xapi = myxapi()
self.xcount = 0
# built-in to run api
self.xapi.run()
# answer to this question
......
# then finally
def xapi_error(self, regId, errorCode, errorString):
print("Error:", errorCode)
self.xcount += 1
......
您能用输入处理第一个error()调用吗
然后,您可以执行以下操作:
这就是你要找的吗
如果你不打电话给
在error函数中,它不会调用重写的方法
相关问题 更多 >
编程相关推荐