[Tutor] classes and the deepcopy function

bob gailer bgailer at alum.rpi.edu
Sat Jan 5 16:18:07 CET 2008


Michael wrote:
> Hi Michael
>
> Thanks for the quick reply, I think I get it. So becuase I did not 
> declare them withing the init method using self they are shared by every 
> object that is created, even completely brand new ones?
>
> Is it normal practice to declare your variables in a class? I notice 
> that you don't have to, you can create them as you go, but i thought 
> declaring and initialising them in the class would make it easier to 
> understand and see what the class is for and should contain.
And now for the rest of the story:

class Foo:
  a = 0
  b = []

f = Foo()
f.a = 3
f.b.append(1)

g = Foo()
g.a = 4
g.b.append(2)

print f.a, g.a # 3 4
print f.b, g.b # [1, 2] [1, 2]

[snip]


More information about the Tutor mailing list