Lists are weird when they are instance members

Mathias Waack M.Waack at gmx.de
Sat Jan 3 17:38:20 EST 2004


python newbie wrote:

> test.py
> ----------------
> 
>  global_filegroup_array = []  # array of filegroup objects
> 
>  class FileGroup:
>        a = 0
>        mylist = []      # here it is

This is a class variable, not a member or instance variable. Make
sure you know the difference. 

>        def  put_stuff_in_my_list(self, anyfile):
>               self.mylist.append( get just a single string from
>               file)   

This line creates a member variable "mylist" of the current instance
"self" of class "FileGroup" as a copy of the class variable
"FileGroup.mylist". 

Just create your member variables in the c'tor function __init__.
This should solve all your problems (ok, at least this problem;)

And btw. Python in a nutshell (I haven't read the other books you've
mentioned) explains the differences between class and member
variables. 

Mathias



More information about the Python-list mailing list