Reputation: 2300
How would I more efficiently combine these two methods into one?
They have identical structure but different parameters ('key_A', 'key_B') and different storage variables (self.storage_a, self.storage_b)
I could make key_X be an input to a generic method, but it seems tacky to pass in self.storage_X when self is already being passed.
def method_a(self):
some_list = list(irrelevant_extraction_function('key_A', self.some_dict))
self.storage_a = [item['address'] for item in some_list]
def method_b(self):
some_list = list(irrelevant_extraction_function('key_B', self.some_dict))
self.storage_b = [item['address'] for item in some_list]
Upvotes: 0
Views: 58
Reputation: 5383
It may be easier to just combine the storage into a dictionary within your own class ...
self.storage = {'key_A':[], 'key_B':[]}
Then, use one function ...
def method(self, key):
some_list = list(irrelevant_extraction_function(key, self.some_dict))
self.storage[key] = [item['address'] for item in some_list]
Upvotes: 3
Reputation: 181
maybe (non tested)
def method(self, key):
some_list = list(irrelevant_extraction_function(key, self.some_dict))
setattr(self, 'storage_{}'.format(key.lower()[-1], [item['address'] for item in some_list])
Upvotes: 1
Reputation: 591
You can try:
def combined_meethod(self, key):
some_list = list(irrelevant_extraction_function(key, self.some_dict))
if key == "key_A":
self.storage_a = [item['address'] for item in some_list]
elif key == "key_B":
self.storage_b = [item['address'] for item in some_list]
Upvotes: 1