Prev: Renaming of files in OS directory
Next: DjangoCon 2010
From: Costin Gament on 8 Aug 2010 09:32 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? Thank you,
From: Roald de Vries on 8 Aug 2010 09:59 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
From: Costin Gament on 8 Aug 2010 10:14 Thank you for your answer, but it seems I didn't make myself clear. Take the code: class foo: a = 0 b = 0 c1 = foo() c1.a = 5 c2 = foo() print c2.a 5 Somehow, when I try to acces the 'a' variable in c2 it has the same value as the 'a' variable in c1. Am I missing something? On Sun, Aug 8, 2010 at 4:59 PM, Roald de Vries <downaold(a)gmail.com> wrote: > > 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 > > -- > http://mail.python.org/mailman/listinfo/python-list >
From: Roald de Vries on 8 Aug 2010 10:38 On Aug 8, 2010, at 4:14 PM, Costin Gament wrote: > Thank you for your answer, but it seems I didn't make myself clear. You could have been clearer in your first post, yeah. > Take the code: > class foo: > a = 0 > b = 0 > c1 = foo() > c1.a = 5 > c2 = foo() > print c2.a > 5 > > Somehow, when I try to acces the 'a' variable in c2 it has the same > value as the 'a' variable in c1. Am I missing something? I can't reproduce this. Which version are you using? > On Sun, Aug 8, 2010 at 4:59 PM, Roald de Vries <downaold(a)gmail.com> > wrote: >> >> Your problem probably is that a and b are class variables; And class variables are not instance variables. >> c1 and c2 are >> different objects (in your terminology: they point to different >> instances). I still suspect that this is the problem. In Python, classes are objects (instances of another class) too. In your class, you assign 0 to the variables foo.a and foo.b. >> See http://docs.python.org/tutorial/classes.html#class-objects for >> more >> info. So: > class foo: > a = 0 creates a class variable foo.a and set it to 0 > b = 0 creates a class variable foo.b and set it to 0 > c1 = foo() creates a new foo that can be referenced as c1 > c1.a = 5 creates an instance variable c1.a and set it to 5 > c2 = foo() creates a new foo that can be referenced as c2 > print c2.a there is no instance variable c2.a, so the class variable foo.a is referenced > 5 I get 0 here. Cheers, Roald
From: Steven D'Aprano on 8 Aug 2010 10:49
On Sun, 08 Aug 2010 17:14:08 +0300, Costin Gament wrote: > Thank you for your answer, but it seems I didn't make myself clear. Take > the code: > class foo: > a = 0 > b = 0 > c1 = foo() > c1.a = 5 > c2 = foo() > print c2.a > 5 Incorrect. >>> class foo: .... a = 0 .... b = 0 .... >>> c1 = foo() >>> c1.a = 5 >>> c2 = foo() >>> print c2.a 0 -- Steven |