简体   繁体   English

如何使用“ var []。function()”调用方调度?

[英]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.

相关问题 使用泛型函数根据调用者执行操作? - Use a generic function to do stuff based on the caller? 有没有办法声明 function 应该使用调用者的 scope ? - Is there a way to declare that a function should use the scope of the caller? 如何找出Python中嵌套函数的调用者 - How to figure out caller of the nesting function in Python 如何使用多个 inheritance 和多态调度? - How to use multiple inheritance and polymorphic dispatch? 如何在Python中使用inspect从被呼叫者获取呼叫者的信息? - How to use inspect to get the caller's info from callee in Python? 如何获得对C api函数的python调用程序的引用 - how to get a reference to python caller of a C api function 如何在python中另一个类的函数中获取调用者类名? - How to get the caller class name inside a function of another class in python? 如何应用以调用者为基础的函数 - how to apply a function which takes the caller as its arugment 如何在函数中修改pandas DataFrame,以便调用者可以看到更改? - How to modify a pandas DataFrame in a function so that changes are seen by the caller? 如何设置接受 function 调用者提供的参数的装饰器? - How to set up decorator that takes in argument provided by the function caller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM