我不知道如何有效地表达我的问题,但我会尽力的。我希望能够使用'for'语句遍历字典并访问以前生成的'self'项。就像我说的,这个问题很难回答。你知道吗
我发现我可以使用exec()来做这件事,但是我被告知除非绝对必要,否则不要使用exec()。另外,我意识到这个例子在技术上是无用的,但它是我需要的一个非常简化的版本。你知道吗
global counter
counter = 0
class GUI:
def __init__(self):
self.stuff = ["foo","bar","fooest","barest"]
for i in self.stuff:
self.process(i)
self.printAll()
def process(self,i):
global counter
counter += 1
self.__dict__.update({"ex{}".format(counter):i})
def printAll(self):
global counter
while counter > 0:
exec("print(self.ex{})".format(counter))
counter -= 1
GUI()
这可以工作;printAll(self)可以打印self.ex1通过ex4。有没有不使用exec()的方法?。请帮帮我!你知道吗
我希望这适合你的情况
相关问题 更多 >
编程相关推荐