Class instance problem?

Riccardo Attilio Galli riccardo_cut-me at cut.me.sideralis.net
Fri May 14 12:31:06 EDT 2004


On Thu, 13 May 2004 08:04:29 +0200, Miki Tebeka wrote:

> Hello Zhao,
> 
>> class aclass:
>>     num = 0
>>     l = [[],[]]
>> 
>> a = aclass()
>> b = aclass()
>> a.l.append(1)
>> print "a.l",a.l
>> print "b.l",b.l
>> 
>>   My expectation is that a,b are separate objects, but appending to
>> a's l also appends to b's l. Why does this occur? On the other hand,
>> the command
>> 
>> a.num = 1
>> 
>> doesn't change b.num's. This is inconsistency is driving me crazy.
>> What am I doing wrong?
> Several things :-)
> First, "num" and "l" are both *class* properties and not object 
> properties. The right way to do this is:
> class aclass:
>      def __init__(self):
>          num = 0
>          l = [[], []]

You surely intended:

class aclass:
    def __init__(self):
        self.num = 0
        self.l = [[], []]

(for Zhao's mental sanity)

Riccardo

-- 
-=Riccardo Galli=-

 _,e.
s~  ``
 ~@.   ideralis Programs
.   ol 
 `**~  http://www.sideralis.net



More information about the Python-list mailing list