Hi DS, 

I'm not sure what language you've dealt with before, but if it was Java or
C++, then you've run into Python's variant of static members.

class X(object):

    classMember = True

    def __init__(self):
        self.instanceMember = True


Does that make it any clearer? As to the why, I'd ask Guido Van Rossum. ;)





-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of DS
Sent: Monday, 7 November 2005 1:20 p.m.
To: tutor@python.org
Subject: [Tutor] Object instances


I had thought I was developing a clue regarding objects in Python, but I ran
across an oddity today that I didn't expect.  It had seemed to me that once
an instance was created, it would have a separate data pool from any other
instance of the same object.  It would share methods, of course, but the
data assigned to it would be totally separate.  After running into peculiar
errors I now see how this is not totally true, but I would like to
understand why not.  I think I'm missing something kind of fundamental here.

The following is my toy example:


In [1]: class test(object):
   ...:     testlist = []
   ...:

In [2]: c1 = test()

In [3]: c1.testlist.append(0)

In [4]: c2 = test()

In [5]: c2.testlist
Out[5]: [0]

In this example, I create two instances of the object test, assigned data to
c1.testlist and it shows up in the other instance, c2. 
Typically, I have been assigning values within an object from being passed
in via __init__, but it seemed like it wasn't really necessary for this
particular kind of object.  In my next example, I show the creation of
testlist via an __init__ and all goes as I expected.


In [10]: class test(object):
   ....:     def __init__(self):
   ....:         self.testlist = []
   ....:

In [11]: c1 = test()

In [12]: c2 = test()

In [13]: c1.testlist.append(0)

In [14]: c2.testlist
Out[14]: []

So, I can see the error of my ways.  I can also see that this behavior gives
me an opportunity to globally change a value in all of the object instances,
if I ever had to do something like that.  I just don't have a clue as to why
objects were designed this way.

Can anyone point me in the right direction?

Thanks

ds




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

A new monthly electronic newsletter covering all aspects of MED's work is now 
available.  Subscribers can choose to receive news from any or all of seven 
categories, free of charge: Growth and Innovation, Strategic Directions, Energy 
and Resources, Business News, ICT, Consumer Issues and Tourism.  See 
http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central & local government 
services

Any opinions expressed in this message are not necessarily those of the 
Ministry of Economic Development. This message and any files transmitted with 
it are confidential and solely for the use of the intended recipient. If you 
are not the intended recipient or the person responsible for delivery to the 
intended recipient, be advised that you have received this message in error and 
that any use is strictly prohibited. Please contact the sender and delete the 
message and any attachment from your computer.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to