简体   繁体   English

从类变量调用方法/函数

[英]calling a method/function from a class variable

lets say I have a class like: 可以说我有一个像这样的课程:

class Foo:

   foo_list = [{'foo1':foo1,'foo2':foo2}] #issue here..Can't use foo1 or self.foo1 here

   def __init__(self):
       pass

   def run(self):
     responses = [func() for func_name, func in Foo.foo_list.items()]

   def foo1(self):
    return True

So basically, I want to use class methods in class variable as well.. How do i do this? 因此,基本上,我也想在类变量中使用类方法。我该怎么做? Am I making any sense? 我说得通吗

Well, you can actually ... you just need to reorder them so that they are defined by the time you use them: 好吧,实际上,您可以...只需要对它们重新排序,以便在使用它们时定义它们:

class Foo:

   def run(self):
      responses = [func(self) for func_name, func in Foo.foo_dict.items()]

   def foo1(self):
      return True

   def foo2(self):
      return False

   foo_dict = {'foo1':foo1,'foo2':foo2}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM