Python 使用字典控制方法流

Python 使用字典控制方法流,python,methods,dictionary,Python,Methods,Dictionary,我有一个包含几个方法的类填充 根据输入,我希望在类Population的实例上按给定顺序运行该方法 更准确地说,我试图实现的目标与使用类似: stuff = input(" enter stuff ") dico = {'stuff1':functionA, 'stuff2':functionC, 'stuff3':functionB, 'stuff4':functionD} dico[stuff]() 除了函数A、函数B等。。。是方法而不是函数: order_type = 'a' cla

我有一个包含几个方法的类填充

根据输入,我希望在类
Population
的实例上按给定顺序运行该方法

更准确地说,我试图实现的目标与使用类似:

stuff = input(" enter stuff ")

dico = {'stuff1':functionA, 'stuff2':functionC, 'stuff3':functionB, 'stuff4':functionD}

dico[stuff]()
除了函数A、函数B等。。。是方法而不是函数:

order_type = 'a'
class Population (object):
    def __init__(self,a):
        self.a = a

    def method1 (self):
        self.a = self.a*2
        return self

    def method2 (self):
        self.a += 2
        return self

    def method3 (self,b):
        self.a = self.a + b
        return self

if order_type=='a':
    order = {1:method1, 2:method2, 3:method3}
elif order_type=='b':
    order = {1:method2, 2:method1, 3:method3}
else :
    order = {1:method3, 2:method2, 3:method1}

my_pop = Population(3)

while iteration < 100:
    iteration +=1
    for i in range(len(order)):
        method_to_use = order[i]
        my_pop.method_to_use() # But obviously it doesn't work!
订单类型='a' 类填充(对象): 定义初始化(self,a): self.a=a def方法1(自身): self.a=self.a*2 回归自我 def方法2(自身): 自我评价a+=2 回归自我 def方法3(自身,b): self.a=self.a+b 回归自我 如果订单类型=='a': 顺序={1:method1,2:method2,3:method3} elif订单类型=='b': 顺序={1:method2,2:method1,3:method3} 其他: 顺序={1:method3,2:method2,3:method1} my_pop=人口(3) 迭代<100时: 迭代次数+=1 对于范围内的i(len(order)): 方法使用=顺序[i] 我的pop.method_to_use()! 希望我的问题已经讲清楚了!
注意,我的一个方法需要两个参数

使用
getattr

order = {1:'method1', 2:'method2', 3:'method3'} #values are strings
...
method_to_use = order[i]
getattr(mypop, method_to_use)()

使用
getattr

order = {1:'method1', 2:'method2', 3:'method3'} #values are strings
...
method_to_use = order[i]
getattr(mypop, method_to_use)()

将实例作为第一个参数显式传递:

method_to_use = order[i]
method_to_use(my_pop)
完整工作代码:

order_type = 'a'
class Population (object):
    def __init__(self,a):
        self.a = a

    def method1 (self):
        self.a = self.a*2
        return self

    def method2 (self):
        self.a += 2
        return self

    def method3 (self):
        self.a = 0
        return self

if order_type=='a':
    order = [Population.method1, Population.method2, Population.method3]
elif order_type=='b':
    order = [Population.method2, Population.method1, Population.method3]
else :
    order = [Population.method3, Population.method2, Population.method1]

my_pop = Population(3)

while iteration < 100:
    iteration +=1
    for method_to_use in order:
        method_to_use(my_pop)

()
是一个空元组,因此
*args
将不扩展为其他参数,而
(_参数)
是一个1元素元组,将参数传递给方法。

将实例作为第一个参数显式传递:

method_to_use = order[i]
method_to_use(my_pop)
完整工作代码:

order_type = 'a'
class Population (object):
    def __init__(self,a):
        self.a = a

    def method1 (self):
        self.a = self.a*2
        return self

    def method2 (self):
        self.a += 2
        return self

    def method3 (self):
        self.a = 0
        return self

if order_type=='a':
    order = [Population.method1, Population.method2, Population.method3]
elif order_type=='b':
    order = [Population.method2, Population.method1, Population.method3]
else :
    order = [Population.method3, Population.method2, Population.method1]

my_pop = Population(3)

while iteration < 100:
    iteration +=1
    for method_to_use in order:
        method_to_use(my_pop)

()
是一个空元组,因此
*args
将不扩展为其他参数,而
(u参数,)
是一个将参数传递给方法的1元素元组。

您可以使用
操作符。methodcaller

from operator import methodcaller
method_to_use = methodcaller('method' + str(i))
method_to_use(my_pop)

您可以使用
operator.methodcaller

from operator import methodcaller
method_to_use = methodcaller('method' + str(i))
method_to_use(my_pop)

谢谢你们的帮助!如果我需要为方法3(但不是其他方法)传递第二个参数呢?谢谢大家的帮助!如果我需要为方法3(但不是其他方法)传递第二个参数呢?@Keyser是的,你是对的,我也可以使用一个列表,但它不会对我遇到的问题有太大改变。谢谢你的回答。但我现在意识到你的解决方案还有第二个问题。如果我只需要为三个方法中的一个添加第二个参数,该怎么办?我更新了我的帖子来举例说明我的问题。我可以用if-else语句来做这件事@凯瑟:是的,你是对的,我也可以用一个列表,但它不会对我遇到的问题有太大的改变。谢谢你的回答。但我现在意识到你的解决方案还有第二个问题。如果我只需要为三个方法中的一个添加第二个参数,该怎么办?我更新了我的帖子来举例说明我的问题。我可以用if-else语句来做这件事!