Static properties
Pete Shinners
pete at shinners.org
Thu Aug 26 10:57:28 EDT 2004
Per Erik Stendahl wrote:
> Is it possible to define "static properties" in a class?
Variables defined at the class object level are what you consider
"static" in C++. One thing that can trip people up is that these static
attributes can be referenced from both the Class object and any Instance
objects. But assignment to the static attribute can only happen on the
Class object, otherwise the Instance will "shadow" the static attribute.
A simple class can demonstrate...
class Example(object):
alpha = 1
beta = 2
def __init__(self):
Example.cappa = Example.alpha + self.beta
self.theta = 4
x = Example()
Example.alpha == x.alpha #both are 1
x.beta = 42
Example.beta != x.beta #now each have their own 'beta'
Example.theta #AttributeError
More information about the Python-list
mailing list