don't understand instanse creation

Dan Sommers me at privacy.net
Tue Aug 9 16:00:47 CEST 2005


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

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list