2

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)
asked May 12, 2017 at 19:02
1
  • 1
    You are not actually invoking 'custom_method' so it's hard to know what you are expecting Commented May 12, 2017 at 19:16

1 Answer 1

3

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']
answered May 12, 2017 at 19:18
Sign up to request clarification or add additional context in comments.

4 Comments

might be worth noting that instead of introducing descriptors you could use getattr(self, custom_method)()
Thank you @Tnerual
@Tnerual I am instantiating the object as a value for json dictionary key. Is there any way I can achieve without doing f = Foo('custom_method_1', 2) f.call_method_num_times()
@new_kid_07. The best is probably to ask another question, refering to this one so as to give the context. If you have this question, it means that the wider community is very likely to have it too.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.