Class initialization

Roald de Vries downaold at gmail.com
Sun Aug 8 09:59:56 EDT 2010


On Aug 8, 2010, at 3:32 PM, Costin Gament wrote:
> Hi there.
> I'm kind of a beginner with Python (and programming in general). My
> problem is with initializing a class. Let's say I've defined it like
> this:
>
> class foo:
>   a = 0
>   b = 0
>
> and later I'm trying to initialize two different classes like this:
> c1 = foo()
> c2 = foo()
>
> The problem I have is that c1 and c2 tend to point to the same
> instance, like a weird c-like pointer. Please tell me, what am I doing
> wrong?

Your problem probably is that a and b are class variables; c1 and c2  
are different objects (in your terminology: they point to different  
instances).

See http://docs.python.org/tutorial/classes.html#class-objects for  
more info.

Cheers, Roald




More information about the Python-list mailing list