Python 运行列表中的函数,指定的函数除外
是否有一种方法可以从另一个方法运行方法中的函数列表,同时跳过列表中的1个指定函数 例如,我有contentList方法:Python 运行列表中的函数,指定的函数除外,python,python-3.x,Python,Python 3.x,是否有一种方法可以从另一个方法运行方法中的函数列表,同时跳过列表中的1个指定函数 例如,我有contentList方法: def contentList(self): methods = [self.title, self.containerDIV, self.heading, self.stopSection, self.offlineSection, self.onlineSection, self.endDIV, self.otherImages] for m in m
def contentList(self):
methods = [self.title, self.containerDIV, self.heading, self.stopSection, self.offlineSection, self.onlineSection, self.endDIV, self.otherImages]
for m in methods:
m()
def test(self):
self.contentList()
# skip over stopSection
在跳过或不运行self.stop部分时,有没有办法从test方法运行这些方法
更新
以下是我正在尝试的更多内容:
#**************************** BEGIN OF HTML CONTENT ****************************
def title(self):
self.wfile.write(bytes("<div id='title'><img src='../images/title.png'></div>", "utf8"))
def containerDIV(self):
self.wfile.write(bytes("<div id='container'", "utf8"))
def endDIV(self):
self.wfile.write(bytes("</div>", "utf8"))
#**************************** END OF HTML CONTENT ****************************
def contentList(self, skip_name): # All content functions in list
methods = [self.title, self.containerDIV, self.endDIV]
for m in methods:
if m.__name__ != skip_name:
m()
def narrow(self):
try:
self.contentList('title') # removed title
return
# GET
def do_GET(self):
try:
self.contentList('none') # I added none here because I want to keep title
return
#************************HTML内容的开头****************************
def标题(自我):
self.wfile.write(字节(“,“utf8”))
def容器IV(自身):
self.wfile.write(字节(“)根据您的作用域,您可以在函数执行之前临时更改其定义
全球范围
def t1():
print('t1')
def t2():
print('t2')
def contentList():
methods = [t1, t2]
for m in methods:
m()
def test():
global t1
def fake(): pass
temp = t1
t1 = fake
contentList()
t1 = temp
test()
def test(self):
def fake(): pass
temp = self.stopSection
self.stopSection = fake
self.contentList()
self.stopSection = temp
类别范围
def t1():
print('t1')
def t2():
print('t2')
def contentList():
methods = [t1, t2]
for m in methods:
m()
def test():
global t1
def fake(): pass
temp = t1
t1 = fake
contentList()
t1 = temp
test()
def test(self):
def fake(): pass
temp = self.stopSection
self.stopSection = fake
self.contentList()
self.stopSection = temp
您可以通过函数名的\uuuu name\uuuu
属性来检查函数名。因此,在您的情况下,您可以执行以下操作:
class SomeClass:
def contentList(self, skip_method_name):
methods = [self.title, self.containerDIV, self.heading, self.stopSection, self.offlineSection, self.onlineSection, self.endDIV, self.otherImages]
for m in methods:
if m.__name__ != skip_method_name:
m()
def test(self):
self.contentList('stopSection')
让我提供一个更简明的例子
class SomeClass:
def method_a(self):
print('method_a')
def method_b(self):
print('method_b')
def method_runner(self, skip_name):
for m in [self.method_a, self.method_b]:
if m.__name__ != skip_name:
m()
示例用法:
>>> some_obj = SomeClass()
>>> some_obj.method_runner('')
method_a
method_b
>>> some_obj.method_runner('method_a')
method_b
>>> some_obj.method_runner('method_b')
method_a
>>> some_obj = SomeClass()
>>> some_obj.method_runner(None)
method_a
method_b
>>> some_obj.method_runner(some_obj.method_a)
method_b
>>> some_obj.method_runner(some_obj.method_b)
method_a
正如@Jeronimo所指出的,我们可以使用方法运行程序
方法中的实际函数,并完全绕过\uuuu名称
调用
class SomeClass:
def method_a(self):
print('method_a')
def method_b(self):
print('method_b')
def method_runner(self, skip_method):
for m in [self.method_a, self.method_b]:
if m != skip_method:
m()
示例用法:
>>> some_obj = SomeClass()
>>> some_obj.method_runner('')
method_a
method_b
>>> some_obj.method_runner('method_a')
method_b
>>> some_obj.method_runner('method_b')
method_a
>>> some_obj = SomeClass()
>>> some_obj.method_runner(None)
method_a
method_b
>>> some_obj.method_runner(some_obj.method_a)
method_b
>>> some_obj.method_runner(some_obj.method_b)
method_a
您是如何指定它的?按列表中的位置?还是按标识?两者都有可能,但标识一可能会对方法很棘手。它可能是其中的任何一种。我从许多不同的方法运行contentList,根据情况,我可能需要跳过其中一个我不想使用的Works!还有一件事,当我尝试和cal时l self.contentList()在需要运行所有列表的其他方法中,我得到一个类型错误,该错误要求在self.contentList()中提供一个位置参数。我通过编写self.contentList(无)来测试它在我的另一个方法中,它起了作用。但这是最好的实践吗?实例方法的第一个参数是对它绑定到的对象的引用e。无论调用它时它是多么隐式。与其在函数定义中添加self作为第一个参数,不如直接将函数作为参数传递,然后c见鬼,如果是循环中的这个问题?@bla抱歉,我到底应该在哪里添加self?抱歉。我有点困了。现在已经很晚了。我在前面的回答中误解了你的问题。你能给我看一些你的代码吗,这样我就可以重现这种行为吗?顺便说一句,我还没有想过@Jeronimo。我会在早上的第一件事将它包括在我的答案中宁:)