[英]Python pseudo callback function
我有我的 main.py
import application
def foo(args):
print("Invoked!")
def main():
app = application.Application()
# here i want to connect app.receivedMessage to foo
和我的 application.py
class Application:
def __init__(self):
pass
def receivedMessage(self, args):
# somehow call foo(args)
基本上我如何调用 foo 并在调用 receivedMessage 时传递参数。 我在应用程序 class 中还有其他部分,它们侦听 usb 消息并调用“receivedMessage”
谢谢
您可以将foo
function 传递给您的Application
object:
class Application:
def __init__(self, foo):
self.foo = foo
def receivedMessage(self, args):
self.foo(args)
将 function 作为参数传递也可以。
#!python3
class Application:
def __init__(self):
pass
def receivedMessage(self,args,**kwargs):
if kwargs and kwargs['foo']:
foo=kwargs['foo']
foo(args)
def foo(args):
print('Invoked')
if __name__=='__main__':
app = Application()
app.receivedMessage("haha", foo=foo) # works
app.receivedMessage("haha") # doesn't raise error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.