Class instance problem?

Zhao Huang zhuang at princeton.edu
Thu May 13 00:43:25 EDT 2004


Hi,

  I'm new to python and I've come across a problem with my objects
corrupting each other.

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?

Thanks for any help



More information about the Python-list mailing list