Is a list static when it's a class member?
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 10 14:54:23 EDT 2006
glue wrote:
> I have a class with a list member and the list seems to behave like
> it's static while other class members don't. The code...
*all* class attributes are shared by all instances. however, instance
attributes hide class attributes with the same name.
in your case, you're hiding the "name" class attribute by creating an
instance attribute with the same name in the "__init__" method, but
you're modifying the shared "data" attribute in the "append" method.
if you want separate instances to use separate objects, make sure you
create new objects for each new instance. see Tim's reply for how to
do that.
</F>
> class A:
> name = ""
> data = []
> def __init__(self, name):
> self.name = name
> def append(self, info):
> self.data.append(info)
> def enum(self):
> for x in self.data:
> print "\t%s" % x
More information about the Python-list
mailing list