Class level variables in Python

Gerrit Holl gerrit at nl.linux.org
Thu Aug 28 05:58:38 EDT 2003


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

Class attributes are for each object, instance attributes only
for one object. E.g.:

class Rectangle(object):
    angle_size = 90
    def __init__(self, x, y):
        self.horizontal = x
        self.vertical = y

(Note that classes are Capitalized).

Now, each Rectangle han an attribute angle_size (which can even
be made read-only, but that's something different), and 'horizontal'
and 'vertical'. This would be the correct approach; initializing
attributes on startup should only be done if they depend on the
call or other "environmental" factors, or should be different for
each instance.

Hope this helps.
Hope it's correct ;)

Gerrit.

-- 
178. If a "devoted woman" or a prostitute to whom her father has given
a dowry and a deed therefor, but if in this deed it is not stated that she
may bequeath it as she pleases, and has not explicitly stated that she has
the right of disposal; if then her father die, then her brothers shall
hold her field and garden, and give her corn, oil, and milk according to
her portion, and satisfy her. If her brothers do not give her corn, oil,
and milk according to her share, then her field and garden shall support
her. She shall have the usufruct of field and garden and all that her
father gave her so long as she lives, but she can not sell or assign it to
others. Her position of inheritance belongs to her brothers.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list