【发布时间】:2017-04-17 19:51:18
【问题描述】:
我在某处读到“如果 python 找不到实例变量,它将尝试返回具有相同名称的类变量的值”
例如
class Sample:
pi = 10
现在
x1 = Sample()
x2 = Sample()
x1.pi # returns 10
x2.pi # returns 10
x1.pi = 20 # change the value of class variable
x1.pi # return 20 (OK)
x2.pi # still returns 10 :(
Sample.pi # returns 10 :(
发生了什么??
【问题讨论】:
-
pi甚至没有在你的类中定义 -
对不起@lmiuelvargasf 的错字,我编辑了帖子
标签: python