Class level variables in Python

Sean Ross sross at connectmail.carleton.ca
Wed Aug 27 20:38:20 EDT 2003


"Brian Munroe" <bmunroe at tribador.nu> wrote in message
news:c16d085c.0308271543.3dfc581e at posting.google.com...
> I was wondering if someone could explain if there is any difference
> between initalizing your object attributes up in the __init__
> constructor or setting them up as (I am guessing at the name here)
> object level variables (like z)

Hi.
Yes there is a difference. One is an instance attribute, the other is a
class attribute:

>>> class C:
...  attr = 1
...  def __init__(self):
...   self.attr = 2
...
>>> c = C()
>>> print c.attr
2
>>> print c.__class__.attr
1
>>>

HTH
Sean






More information about the Python-list mailing list