gpt4 book ai didi

python - 运行列表中除指定函数之外的函数

转载 作者:行者123 更新时间:2023-11-30 22:18:47 25 4
gpt4 key购买 nike

有没有办法从另一个方法运行一个方法中的函数列表,同时跳过列表中的 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 methods:
m()
def test(self):
self.contentList()
# skip over stopSection

有没有办法从测试方法运行这些方法,同时跳过或不运行 self.stopSection?

更新

以下是我正在尝试的更多内容:

#**************************** 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

最佳答案

根据您的范围,您可以在执行之前临时更改函数的定义。

全局范围

    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

关于python - 运行列表中除指定函数之外的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49270059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com