从字符串调用方法

发布于 2024-08-13 09:35:35 字数 282 浏览 3 评论 0原文

如果我有一个 Python 类,并且想根据变量调用其中的函数,我该怎么做?我想象以下可以做到这一点:

class CallMe: # Class

   def App(): # Method one

      ...

   def Foo(): # Method two

      ...

variable = "App" # Method to call

CallMe.variable() # Calling App()

但它不能。还有其他方法可以做到这一点吗?

If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:

class CallMe: # Class

   def App(): # Method one

      ...

   def Foo(): # Method two

      ...

variable = "App" # Method to call

CallMe.variable() # Calling App()

But it couldn't. Any other way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

能否归途做我良人 2024-08-20 09:35:35

你可以这样做:

getattr(CallMe, variable)()

getattr 是一个内置方法,它返回对象的命名属性。本例中的值是一个方法对象,您可以使用 () 调用

You can do this:

getattr(CallMe, variable)()

getattr is a builtin method, it returns the value of the named attributed of object. The value in this case is a method object that you can call with ()

毁梦 2024-08-20 09:35:35

您可以使用 getattr,也可以将绑定或未绑定的方法分配给变量。绑定方法与类的特定实例绑定,而未绑定方法与类绑定,因此您必须将实例作为第一个参数传递。

例如

class CallMe:
    def App(self):
        print "this is App"

    def Foo(self):
        print "I'm Foo"

obj = CallMe()

# bound method:
var = obj.App
var()         # prints "this is App"

# unbound method:
var = CallMe.Foo
var(obj)      # prints "I'm Foo"

You can use getattr, or you can assign bound or unbound methods to the variable. Bound methods are tied to a particular instance of the class, and unbound methods are tied to the class, so you have to pass an instance in as the first parameter.

e.g.

class CallMe:
    def App(self):
        print "this is App"

    def Foo(self):
        print "I'm Foo"

obj = CallMe()

# bound method:
var = obj.App
var()         # prints "this is App"

# unbound method:
var = CallMe.Foo
var(obj)      # prints "I'm Foo"
苏别ゝ 2024-08-20 09:35:35

您的班级已被宣布为“旧式班级”。我建议你把所有的课程都设为“新式课程”。

新旧类之间的区别在于新式类可以使用继承,但您可能不会立即需要继承。但这是一个值得养成的好习惯。

创建新样式类所需要做的就是:使用 Python 语法表明它继承自“object”。您可以通过在类名后面加上括号并将名称 object 放在括号内来实现此目的。就像这样:

class CallMe(object): # Class

   def App(): # Method one

      ...

   def Foo(): # Method two

      ...

正如我所说,您可能不需要立即使用继承,但这是一个值得养成的好习惯。 StackOverflow 上有几个问题,大意是“我正在尝试做 X,但它不起作用”,结果发现这个人编写了一个旧式的类。

Your class has been declared as an "old-style class". I recommend you make all your classes be "new-style classes".

The difference between the old and the new is that new-style classes can use inheritance, which you might not need right away. But it's a good habit to get into.

Here is all you have to do to make a new-style class: you use the Python syntax to say that it inherits from "object". You do that by putting parentheses after the class name and putting the name object inside the parentheses. Like so:

class CallMe(object): # Class

   def App(): # Method one

      ...

   def Foo(): # Method two

      ...

As I said, you might not need to use inheritance right away, but this is a good habit to get into. There are several questions here on StackOverflow to the effect of "I'm trying to do X and it doesn't work" and it turns out the person had coded an old-style class.

北城半夏 2024-08-20 09:35:35

你的代码看起来不像Python,也许你想这样做?

class CallMe:

    def App(self): #// Method one
        print "hello"

    def Foo(self): #// Method two
        return None

    variable = App #// Method to call

CallMe().variable() #// Calling App()

Your code does not look like python, may be you want to do like this?

class CallMe:

    def App(self): #// Method one
        print "hello"

    def Foo(self): #// Method two
        return None

    variable = App #// Method to call

CallMe().variable() #// Calling App()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文