【发布时间】:2012-02-22 12:07:26
【问题描述】:
我似乎无法理解这里发生了什么:
class testclass:
def __init__(self):
print "new instance"
myList=[]
if __name__ == "__main__":
inst1=testclass()
inst1.myList.append("wrong")
inst2=testclass()
inst2.myList.append("behaviour")
print "I get the",inst2.myList
输出是:
new instance
new instance
I get the ['wrong', 'behaviour']
我原以为 inst1 中的列表对 inst2 中的列表一无所知,但不知何故它看起来像是 myList 的范围超越类的实例化。 我觉得这非常令人不安和令人费解,还是我在这里遗漏了什么?
谢谢!
【问题讨论】:
标签: python scope class-members