Python 调用列表中的方法,但指定的方法除外
我有一个方法列表,我可以用另一个方法同时调用所有的方法 如何调用包含all的方法,同时从列表中删除某些项(或方法)?我当前可以指定一个要忽略的选项。我不知道怎样才能漏掉不止一个。以下是我的工作内容:Python 调用列表中的方法,但指定的方法除外,python,python-3.x,Python,Python 3.x,我有一个方法列表,我可以用另一个方法同时调用所有的方法 如何调用包含all的方法,同时从列表中删除某些项(或方法)?我当前可以指定一个要忽略的选项。我不知道怎样才能漏掉不止一个。以下是我的工作内容: class SomeClass: def method_a(self): print('method_a') def method_b(self): print('method_b') def method_c(self):
class SomeClass:
def method_a(self):
print('method_a')
def method_b(self):
print('method_b')
def method_c(self):
print('method_c')
def method_runner(self, skip_name='' ):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ != skip_name:
m()
现在我可以做到:
>>> some_obj = SomeClass()
>>> some_obj.method_runner('')
method_a
method_b
method_c
>>> some_obj.method_runner('method_a')
method_b
method_c
>>> some_obj.method_runner('method_b')
method_a
method_c
def method_runner(self, *skip_list):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skip_list:
m()
有没有办法做到这一点
class SomeClass:
def method_a(self):
print('method_a')
def method_b(self):
print('method_b')
def method_c(self):
print('method_c')
def method_runner(self, skip_name='', skip_name2='', skip_name3=''):
for m in [self.method_a, self.method_b, self.method_c]:
options = [skip_name, skip_name2, skip_name3]
for o in options:
if m.__name__ != o:
m()
并指定多个方法以获得结果,例如:
>>> some_obj.method_runner('method_a', 'method_c')
method_b
您可以这样做:
>>> some_obj = SomeClass()
>>> some_obj.method_runner('')
method_a
method_b
method_c
>>> some_obj.method_runner('method_a')
method_b
method_c
>>> some_obj.method_runner('method_b')
method_a
method_c
def method_runner(self, *skip_list):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skip_list:
m()
*
表示参数的数量不同
如果不想手动键入方法名称,可以使用以下方法:
def method_runner(self, *skip_list):
methods = [f for f in map(lambda x: getattr(self,x), dir(self)) if callable(f) and f.__name__ != 'method_runner']
for m in methods:
if m.__name__ not in skip_list:
m()
您可以这样做:
>>> some_obj = SomeClass()
>>> some_obj.method_runner('')
method_a
method_b
method_c
>>> some_obj.method_runner('method_a')
method_b
method_c
>>> some_obj.method_runner('method_b')
method_a
method_c
def method_runner(self, *skip_list):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skip_list:
m()
*
表示参数的数量不同
如果不想手动键入方法名称,可以使用以下方法:
def method_runner(self, *skip_list):
methods = [f for f in map(lambda x: getattr(self,x), dir(self)) if callable(f) and f.__name__ != 'method_runner']
for m in methods:
if m.__name__ not in skip_list:
m()
是的,至少有直截了当的方法。最简单的方法是参数accept要跳过的对象容器:
def method_runner(self, skipnames):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skipnames:
m()
您可以通过传递一个适当的容器来调用(在本例中,set
,但list
或tuple
可能会这样做):
但听起来您更喜欢使用可变长度参数:
def method_runner(self, *skipnames):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skipnames:
m(self)
你可以这样称呼它:
SomeClass().method_runner('method_a', 'method_c')
但是,skipnames
现在将始终是一个元组,它可能不具备所需的性能特征
按照@ChristianDean的建议,您可能希望将方法列表设置为类变量:
In [7]: class SomeClass:
...: def method_a(self):
...: print('method_a')
...:
...: def method_b(self):
...: print('method_b')
...:
...: def method_c(self):
...: print('method_c')
...:
...: _my_methods = method_a, method_b, method_c
...:
...: def method_runner(self, skipnames):
...: for m in self._my_methods:
...: if m.__name__ not in skipnames:
...: m(self)
...:
In [8]: SomeClass().method_runner({'method_a', 'method_c'})
method_b
请注意,您必须手动将
self
传递给函数,因为它不是一种方法 是的,至少有直截了当的方法。最简单的方法是参数accept要跳过的对象容器:
def method_runner(self, skipnames):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skipnames:
m()
您可以通过传递一个适当的容器来调用(在本例中,set
,但list
或tuple
可能会这样做):
但听起来您更喜欢使用可变长度参数:
def method_runner(self, *skipnames):
for m in [self.method_a, self.method_b, self.method_c]:
if m.__name__ not in skipnames:
m(self)
你可以这样称呼它:
SomeClass().method_runner('method_a', 'method_c')
但是,skipnames
现在将始终是一个元组,它可能不具备所需的性能特征
按照@ChristianDean的建议,您可能希望将方法列表设置为类变量:
In [7]: class SomeClass:
...: def method_a(self):
...: print('method_a')
...:
...: def method_b(self):
...: print('method_b')
...:
...: def method_c(self):
...: print('method_c')
...:
...: _my_methods = method_a, method_b, method_c
...:
...: def method_runner(self, skipnames):
...: for m in self._my_methods:
...: if m.__name__ not in skipnames:
...: m(self)
...:
In [8]: SomeClass().method_runner({'method_a', 'method_c'})
method_b
请注意,您必须手动将
self
传递给函数,因为它不是一种方法 @KadeWilliams在函数定义签名中表示+1,我还建议将列表[self.method\u a,self.method\u b,self.method\u c]
作为类变量。这样就可以避免每次调用method\u runner
时浪费时间重建相同的列表。如果性能是一个问题,您可以轻松地将元组转换为一个集合。一个类可以有多少个方法?@MadPhysician是的,可能这两种方法都不会有明显的区别。@KadeWilliams在函数定义签名中表示+1,我还建议将列表[self.method\u a,self.method\u b,self.method\u c]
作为类变量。这样就可以避免每次调用method\u runner
时浪费时间重建相同的列表。如果性能是一个问题,您可以轻松地将元组转换为一个集合。“一个类可以有多少种方法?”MadPhysician说,“是的,可能这两种方法都不会有明显的区别。”。