如何在python类中将方法名称作为参数传递

如何在python类中将方法名称作为参数传递

本文介绍了如何在python类中将方法名称作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我的意图是在初始化对象并且要运行方法 num(第二个参数)多次时将方法名称作为参数传递。基本上得到n个结果(如第二个参数所述)。

This is my code, my intention is to pass the method name as a parameter when I initialize the object and I want to run the method 'num' (second argument) of times. Basically get n number of results (as mentioned in 2nd argument).

 class Foo(object):
     faker = Faker()

     def __init__(self,  custom_method, num=1):
         self.values = []
         self.custom_method = custom_method
         self.num = num
         for x in self.num:
             self.custom_method = self.values.append(custom_method)


    def random_first_name(self):
        self.custom_method = self.faker.first.name()
        return self.custom_method

    def random_phone(self):
        self.custom_method = self.faker.random.phone()
        return self.custom_method

    b = Foo(random_first_name, 1)
    c = Foo(random_phone,2)


推荐答案

我想您可能想使用。

I guess that you may want to use the function getattr.

class Foo(object):
    faker = Faker()

    def __init__(self, custom_method, num=1):
        self.custom_method = custom_method
        self.num = num

    @property # Briefly, the property decorator makes the job of calling the callable for you. I.e. There is no need to do self.method(), self.method is enough.
    def random_first_name(self):
        return self.faker.first.name()

    @property
    def random_phone(self):
        return self.faker.random.phone()

    def call_method_num_times(self):
        return [getattr(self, self.custom_method)\
                for _ in range(self.num)]

我无法实例化此类,但是可以如下使用:

I cannot instantiate this class, but this could be used as follows:

>>> foo1 = Foo('random_first_name', 1)
>>> foo1.call_method_num_times()
['John']

>>> foo2 = Foo('random_phone', 2)
>>> foo2.call_method_num_times()
['0123456789', '9876543210']



为了(进一步)以(主观上)更好的方式重组您的班级,我会这样做


To (even more) reorganize your class in a (subjectively) better fashion, I would do

class Foo(object):

    def __init__(self):
        self.faker = Faker()

    @property
    def random_first_name(self):
        return self.faker.first.name()

    @property
    def random_phone(self):
        return self.faker.random.phone()

    def call_method_num_times(self, custom_method, num=1):
        return [getattr(self, custom_method)\
                for _ in range(num)]

因此只允许实例化 Foo

>>> foo = Foo()
>>> foo.call_method_num_times('random_first_name')
['John']
>>> foo.call_method_num_times('random_phone', 2)
['0123456789', '9876543210']



如果您对使用Python本机描述符,您可以将两个方法保留为显式方法。在这种情况下,您将按如下所示定义类 Foo

class Foo(object):

    def __init__(self):
        self.faker = Faker()

    def random_first_name(self):
        return self.faker.first.name()

    def random_phone(self):
        return self.faker.random.phone()

    def call_method_num_times(self, custom_method, num=1):
        return [getattr(self, custom_method)()\
                for _ in range(num)]

使用 Foo

>>> foo = Foo()
>>> foo.call_method_num_times('random_first_name')
['John']
>>> foo.call_method_num_times('random_phone', 2)
['0123456789', '9876543210']

这篇关于如何在python类中将方法名称作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:17