从类中调用python函数字典 [英] Calling python dictionary of function from class

查看:347
本文介绍了从类中调用python函数字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,想创建一个具有字典的类,该字典对应于该类中的不同方法.我目前有:

I'm relatively new to python and would like to make a class that has a dictionary that corresponds to different methods in the class. I currently have :

class Test:
  functions = {"Test1":test1, "Test2":test2}

  def test1(self, arg1):
    print "test1"

  def test2(self, arg1):
    print "test2" + arg1

  def call_me(self, arg):
    self.functions[arg](arg)

然后在main.py中,我有以下内容:

Then in my main.py I have the following:

from Test import Test

t = Test()

t.call_me('Test1')

当我调用此函数时,出现一条错误消息,提示未定义名称test1.谁能告诉我我在做什么错?任何帮助将不胜感激.

When I call this function I get an error saying name test1 is not defined. Can anyone tell me what I am doing wrong? Any help would be greatly appreciated.

推荐答案

您在这里遇到了多个问题.

You've got multiple problems here.

首先,在这一行:

functions = {"Test1":test1, "Test2":test2}

在Python执行此行代码时,没有称为test1test2的东西,因此您将获得直接的NameError.如果您想以这种方式进行操作,则将在所有功能都已定义之后,而不是之前,定义functions .

At the time Python executes this line of code, there is nothing called test1 or test2, so you're going to get an immediate NameError. If you want to do things this way, you're going to have define functions after all of the functions have been defined, not before.

接下来,在同一行上,test1test2在这时是普通的函数.但是,您尝试将它们称为方法,并使用魔术self和其他所有功能来称呼它们.那是行不通的.如果您了解方法的工作原理,那么显然可以在call_me方法中解决此问题:

Next, on the same line, test1 and test2 at this point are plain-old functions. But you're trying to call them as if they were methods, with the magic self and everything. That isn't going to work. If you understand how methods work, it should be obvious that you can work around this in the call_me method:

def call_me(self, arg): self.functions[arg].__get__(self, type(self))(arg)

(在这种情况下,您还可以通过显式传递self作为附加参数来避免.但是在执行此操作之前,请确保您了解原因.)

(In this case, you can also get away with just explicitly passing self as an extra argument. But make sure you understand why before doing that.)

最后,您尝试使用函数test1而不是名称'Test1'调用call_me.大概是因为您创建此映射的全部原因是,您可以使用名称(以动态方式,作为字符串),所以让我们实际使用它们:

Finally, you're trying to call call_me with the function test1, instead of the name 'Test1'. Presumably the whole reason you've created this mapping is so that you can use the names (dynamically, as strings), so let's actually use them:

t.call_me('Test1')


请注意,如果不能使用getattr的唯一原因是要查找的运行时名称与要定义的方法名称不同,则始终可以使用字符串映射来字符串,然后使用getattr查找生成的字符串,从而避免了此处的所有其他问题.从 aruisdante的答案中借用,添加名称查找并记住通过arg:


Note that if the only reason you can't use getattr is that the runtime names you want to look up aren't the same as the method names you want to define, you can always have a map of strings to strings, and look up the resulting strings with getattr, which avoids all the other problems here. Borrowing from aruisdante's answer, adding in the name lookup and remembering to pass arg:

functions = {"Test1": "test1", "Test2": "test2"}

def call_me(self, arg):
    return getattr(self, self.functions[arg])(arg)

这篇关于从类中调用python函数字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆