don't understand instanse creation

Maksim Kasimov maksim.kasimov at gmail.com
Tue Aug 9 16:24:11 CEST 2005


i see, thaks you very match!

Dan Sommers wrote:
> On Tue, 09 Aug 2005 16:39:51 +0300,
> Maksim Kasimov <maksim.kasimov at gmail.com> wrote:
> 
> 
>>Hello,
> 
> 
>>i have a class, such as below.
>>when i try to make instances of the class,
>>fields __data1 and __data2 gets different values: __data1 behaves like private field, __data2 - like static
>>which is the thing i've missed?
> 
> 
>>thanks for help.
> 
> 
>>====================================================
> 
> 
> 
>>import time
>>class my:
> 
> 
>>     __data1 = []
>>     __data2 = []
> 
> 
>>     def __init__(self):
> 
> 
>>         print "__data1: ", self.__data1
>>         print "__data2: ", self.__data2
> 
> 
>>         for i in time.localtime():
>>             self.__data2.append(i)
> 
> 
> The above line changes the existing class attribute __data2, which is
> shared by all instances of this class.
> 
> 
>>         self.__data1 = self.__data2[:]
> 
> 
> The above line creates a new instance attribute __data1, and binds it to
> a copy of __data2.
> 
> 
>>         print "__data1: ", self.__data1
>>         print "__data2: ", self.__data2
> 
> 
> [ example snipped ]
> 
> HTH,
> Dan
> 


-- 
Best regards,
Maksim Kasimov
mailto: maksim.kasimov at gmail.com



More information about the Python-list mailing list