[英]How to use the “var[].function()” caller dispatch?
I am trying to learn how to dispatch an action that is related to an element of a dictionary. 我正在尝试学习如何调度与字典元素相关的动作。
I´ve seen a lot of times the formula dictionary[element].function()
, and there the function calls the element
of the dictionary. 我已经看过很多次公式dictionary[element].function()
,该函数在其中调用字典的element
。
I am trying to do that with this code, but I can not get the working method. 我正在尝试使用此代码执行此操作,但无法获得有效的方法。
Any ideas? 有任何想法吗?
(The last line of the code is the syntax that I want to use) (代码的最后一行是我要使用的语法)
Thank you in advance! 先感谢您!
def function(arg):
print 'Running ' + str(arg)
data={}
data[0]={'name':'Ruben', 'run':function}
data[1]={'name':'David', 'run':function}
print data
data[0].run()
Use data[0]['run']('Hello World!')
. 使用data[0]['run']('Hello World!')
。 This executes the function with the argument Hello World!
这将使用参数Hello World!
执行该函数Hello World!
and prints: 并打印:
Running Hello World!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.