how to pass method name as a parameter in python class

余生颓废 提交于 2019-12-04 20:23:45

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)]

Thus allowing you for instantiating Foo only once

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


If you are not comfortable with the use of the python native property descriptor, you can keep your two methods as explicite ones. In this case, you would define the class Foo as follows
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)]

Which would change nothing in ways of using Foo

>>> foo = Foo()
>>> foo.call_method_num_times('random_first_name')
['John']
>>> foo.call_method_num_times('random_phone', 2)
['0123456789', '9876543210']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!