Class level variables in Python

Brian Munroe bmunroe at tribador.nu
Wed Aug 27 19:43:03 EDT 2003


I am just starting to learn the OO side of Python scripting, and I am
a little confused on the following.  Take the following example class:

>>> class rectangle(object):
	z = 1
	def __init__(self):
		self.x = 2

>>> r = rectangle()
>>> print r.z
1
>>> print r.x
2
>>> r.z = 16
>>> print r.z
16
>>> r.x = 17
>>> print r.x
17
>>>

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)

thanks

-- brian




More information about the Python-list mailing list