In regards to this answer, for a constant static variable, you can use a descriptor. Here's an example:
class ConstantAttribute(object):
'''You can initialize my value but not change it.'''
def __init__(self, value):
self.value = value
def __get__(self, obj, type=None):
return self.value
def __set__(self, obj, val):
pass
class Demo(object):
x = ConstantAttribute(10)
class SubDemo(Demo):
x = 10
demo = Demo()
subdemo = SubDemo()
# should not change
demo.x = 100
# should change
subdemo.x = 100
print "small demo", demo.x
print "small subdemo", subdemo.x
print "big demo", Demo.x
print "big subdemo", SubDemo.x
resulting in ...
small demo 10
small subdemo 100
big demo 10
big subdemo 10
You can always raise an exception if quietly ignoring setting value (pass
above) is not your thing. If you're looking for a C++, Java style static class variable:
class StaticAttribute(object):
def __init__(self, value):
self.value = value
def __get__(self, obj, type=None):
return self.value
def __set__(self, obj, val):
self.value = val
Have a look at this answer and the official docs HOWTO for more information about descriptors.