Is a list static when it's a class member?

Tim Chase python.list at tim.thechases.com
Tue Oct 10 14:50:57 EDT 2006


> 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
> How do i get:
> A:
>         one
>         two
> B:
>         horse
>         bear
> A:
>         one
>         two


class A:
     name = ""
#    data = []  # just move this line
     def __init__(self, name):   #
         self.name = name        #
         self.data = []  #      here
     def append(self, info):
         self.data.append(info)
     def enum(self):
         for x in self.data:
             print "\t%s" % x


It will be given a "fresh" list upon each __init__ call.

-tkc





More information about the Python-list mailing list