84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
认证高级PHP讲师
What you declare is 类变量 not a member variable, which means your value is bound to the type, not the instance.
类变量
Try this
def A(object): def __init__(): self.value = [ ] def append(v): self.value.append(v)
class MyClass(): def __init__(self): self.value=[] def append(self,value): self.value.append(value) def get(self): return self.value instance_a=MyClass() instance_b=MyClass() instance_a.append(100) print instance_b.get() print instance_a.get()
The questioner can take a look at this: https://www.zhihu.com/question/25874136Explanation on static variables and instance variables. As for how to operate, the above two answers have been clearly listed.
实例变量是要在`__init__(self)`中定义的变量。如果没有定义变量,那就会搜索scope上同名变量。
What you declare is
类变量
not a member variable, which means your value is bound to the type, not the instance.Try this
The questioner can take a look at this: https://www.zhihu.com/question/25874136
Explanation on static variables and instance variables.
As for how to operate, the above two answers have been clearly listed.