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 <downa...@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

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to