Hi Tim,

If you have

class Foo(object) :
        x = 0
        y = 1

foo = Foo()

foo.x # reads either instance or class attribute (class in this case)

foo.x = val # sets an instance attribute (because foo is instance not class)

Foo.x = val           # sets a class attribute
foo.__class.__x = val # does the same

this might be sometimes confusing. IMHO, the following is especially nasty:

>>> foo = Foo()
>>> foo.x += 1
>>>
>>> print foo.x
1
>>> print Foo.x
0

although the += operator looks like an inplace add it isn't.
it is just syntactic sugar for foo.x = foo.x + 1.


- harold -


On 13.01.2005, at 07:18, Tim Daneliuk wrote:

I am a bit confused.  I was under the impression that:

class foo(object):
        x = 0
        y = 1

means that x and y are variables shared by all instances of a class.
But when I run this against two instances of foo, and set the values
of x and y, they are indeed unique to the *instance* rather than the
class.

It is late and I am probably missing the obvious. Enlightenment appreciated ...
-- ----------------------------------------------------------------------- -----
Tim Daneliuk [EMAIL PROTECTED]
PGP Key: http://www.tundraware.com/PGP/
--
http://mail.python.org/mailman/listinfo/python-list



--
Everyone is a genius.
It's just that some people are too stupid to realize it.

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

Reply via email to