简体   繁体   English

如何使用python通过字符串名称调用方法?

[英]How do i call a method by a string name using python?

I have the following class; 我有以下课程;

class myStringMethod():
    def __init__(self):
        self.func_list= [('func1','print_func1()'),('func2','print_func2()')]

    def print_func1(self, name):
        print name

    def print_func2(self, name):
        print name

    def call_func_by_name(self):
        for func in self.func_list:
            getattr(self, func[1])('Func Name')

if __name__=='__main__':
    strM = myStringMethod()
    strM.call_func_by_name() #Nothing prints out!

No functions get called out, what am i missing? 没有函数被调用,我缺少什么?

gath 盖特

your self.func_list should be: 您的self.func_list应该是:

self.func_list= [('func1','print_func1'),('func2','print_func2')]

And the way your code is written it will, of course, print 'Func Name' . 当然,编写代码的方式将显示'Func Name' I guess you probably meant to pass func[0] there. 我猜您可能打算将func[0]传递到那里。

Working example: 工作示例:

>>> class myStringMethod():
    def __init__(self):
        self.func_list= [('func1','print_func1'),('func2','print_func2')]

    def print_func1(self, name):
        print(name)

    def print_func2(self, name):
        print(name)

    def call_func_by_name(self):
        for func in self.func_list:
            getattr(self, func[1])('Func Name')

>>> myStringMethod().call_func_by_name()
Func Name
Func Name

maybe offtopic, but maybe your list is actually a dict : 也许是题外话,但也许您的清单实际上是字典:

self.functs = {'func1':'print_func1', ...}

and then call with : 然后致电:

for fn in self.functs.values() : self.__dict__[fn]('Func Name')

or even if you wanted to call all 'func_' functions in your class (no func_list here) : 甚至即使您想调用类中的所有“ func_”函数(此处也没有func_list):

@classmethod
def call_func_by_name(cls)
  for n,f in cls.__dict__.values() : 
     if n.startswith('print_') and hasattr(f,'__call__'): f('Hello')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python脚本正在运行。我有一个方法名称作为字符串。我该如何调用此方法? - Python script is running. I have a method name as a string. How do I call this method? Python:当你只有方法的字符串名称时,如何调用方法? - Python: How do you call a method when you only have the string name of the method? 如何使用转义在Python中用输入字符串替换name1和name2? - How do I replace name1 and name2 with an input string in Python using escaping? 如果我有一个 object,一个字符串中的方法名称和方法接受一个参数,我该如何调用该方法? - If I have an object , a method name in a string and method takes one parameter, how can I call the method? 如何在 python 中调用没有方法名称的方法 - How to call method without method name in python 如何使用字符串动态地按名称调用 Python function? - How to call Python function by name dynamically using a string? 如何在 Python 中获取包含日志记录调用的类的名称? - How do I get the name of the class containing a logging call in Python? 如何在Python中的特定基类上调用方法? - How do I call a method on a specific base class in Python? 如何从不同的脚本调用方法? (Python) - How do I call a method from a different script? (Python) 如何从 Python 中的另一个文件调用类方法? - How do I call a class method from another file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM