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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值