array in class

Marco Mariani marco at sferacarta.com
Tue May 13 06:20:12 EDT 2008


alefajnie wrote:

> class B:
>   this_is_common_for_all_instances = []
> 
>   def __init__(self, v):
>     self.this_is_common_for_all_instances.append(v)
> 
> 
> ----------------
> now I can create some instances of B, but all of them have the same
> array, why

Because you didn't reassign the attribute 
'this_is_common_for_all_instances', but appended to it.

> and how create array in class - normal array, "private variable"

1) it's called a list, not an array
2) you do that in the __init__ method: self.blabla = []
3) still, it won't be a "private" attribute, just an instance attribute



More information about the Python-list mailing list