У меня есть класснекоторая путаница с переменными заявленным питоном классом
>>> class Foo:
... ls=[]
...
>>> f1=Foo()
>>> f2=Foo()
>>> f1.ls.append(1)
>>> f1.ls.append(2)
>>> print f1.ls
[1, 2]
>>> print f2.ls
[1, 2] #I expect its result is empty [], why
>>> f2.ls=[]
>>> print f1.ls
[1, 2]
>>> print f2.ls
[]
# If f1.ls and f2.ls refer to the same list, since i modify f2.ls,
# the f1.ls is empty ,too. Does the statement 'f2.ls=[]' add new attribute
# to f2. Where do f1.ls and f2.ls refer and how it happens
Я хочу использовать один класс и объявить много переменных. Если я надеюсь, что все переменные имеют разные списки. Как мне это сделать
class Foo:
pass
f1=Foo()
f2=oo()
f1.ls=[]
f2.ls=[]
do others
Есть ли еще более простые и лучшие методы. Простите мое невежество для класса python. Заранее спасибо
См. Также http://stackoverflow.com/questions/8701500/python-class-instance-variables-and-class-variables –
Возможный дубликат [Статические переменные класса в Python] (http://stackoverflow.com/ Вопросы/68645/static-class-variables-in-python) – Bakuriu