Ada*_*ose 1 python dictionary python-3.x
我想从字典中调用函数。可能吗?如果是这样,我将如何去做呢?
def function():
pass
my_dic = {
'hello': function()
}
Run Code Online (Sandbox Code Playgroud)
据我了解,您真正的意思是如何将函数引用保留在字典中,以便在通过键从字典中获取函数引用之后再调用它。这是可能的,因为函数是Python中的一流对象:
>>> def function():
... print("Hello World")
...
>>> d = {
... 'hello': function # NOTE: we are not calling the function here - just keeping the reference
... }
>>> d['hello']()
Hello World
Run Code Online (Sandbox Code Playgroud)
与一些用例示例相关的线程:
| 归档时间: |
|
| 查看次数: |
475 次 |
| 最近记录: |