假设我有这样一门课:
class ContentConfigParser(object):
def __init__(self,txt):
try:
with open (txt,'r') as conf:
conflines = conf.readlines()
self.width = conflines[0].strip()
self.height = conflines[1].strip()我就是这样用的:
while 1:
config= ContentConfigParser("settings.txt") #1
#bla bla with config.width
#peh peh with config.height 我在(1)中使用的旧类发生了什么情况?这是一个好的编码吗?
发布于 2019-04-29 08:13:30
您在1处拥有的不是类,而是对象或实例。一旦它们没有剩余的/超出范围的引用,它们将被丢弃(或者标记为垃圾收集器来拾取)。所以每次迭代它们都会被删除。问题是,如果您不能在循环之外创建实例,然后继续在循环中使用它。
https://stackoverflow.com/questions/55899131
复制相似问题