我如何更有效地将这两种方法合并为一种?
它们具有相同的结构但参数不同(' key_A',' key_B')和不同的存储变量(self.storage_a,self.storage_b)
我可以让key_X成为泛型方法的输入,但是当self已经被传递时传递self.storage_X似乎很俗气。
UInt32 audioRouteOverride = enabled ? kAudioSessionOverrideAudioRoute_Speaker : kAudioSessionOverrideAudioRoute_None;
if(AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride),&audioRouteOverride) == 0){
speakerOn = enabled;
return YES;
}
答案 0 :(得分:3)
将存储组合到您自己的类中的字典中可能更容易......
self.storage = {'key_A':[], 'key_B':[]}
然后,使用一个函数......
def method(self, key):
some_list = list(irrelevant_extraction_function(key, self.some_dict))
self.storage[key] = [item['address'] for item in some_list]
答案 1 :(得分:1)
您可以尝试:
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]
答案 2 :(得分:1)
也许(未经测试)
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])