[Tutor] classes and the deepcopy function

Michael python at mrfab.info
Sun Jan 6 11:16:19 CET 2008


Okay

Just when I think I am getting it you throw this in. So why does a 
become local to each variable but b seem global?

Michael

bob gailer wrote:
> 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