Is a list static when it's a class member?
Antoine De Groote
antoine at vo.lu
Tue Oct 10 14:42:32 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...
>
> 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
>
> a = A("A:")
> print a.name
> a.append("one")
> a.append("two")
> a.enum()
> b = A("B:")
> print b.name
> b.append("horse")
> b.append("bear")
> b.enum()
> print a.name
> a.enum()
>
> The output...
> A:
> one
> two
> B:
> one
> two
> horse
> bear
> A:
> one
> two
> horse
> bear
>
> How do i get:
> A:
> one
> two
> B:
> horse
> bear
> A:
> one
> two
>
> Thanks,
>
> glue
>
I can already tell you that this does it:
class A:
name = ""
def __init__(self, name):
self.data = []
self.name = name
def append(self, info):
self.data.append(info)
def enum(self):
for x in self.data:
print "\t%s" % x
Somebody else can explain the reason. I just don't wanna write something
that I'm not sure of. :-)
Regards,
antoine
More information about the Python-list
mailing list