I think you may be able to do this with the metaclass. Since the metaclass can be like a class for the class (if that makes sense). I know you can assign a __call__()
method to the metaclass to override calling the class, MyClass()
. I wonder if using the property
decorator on the metaclass operates similarly. (I haven't tried this before, but now I'm curious...)
[update:]
Wow, it does work:
class MetaClass(type):
def getfoo(self):
return self._foo
foo = property(getfoo)
@property
def bar(self):
return self._bar
class MyClass(object):
__metaclass__ = MetaClass
_foo = 'abc'
_bar = 'def'
print MyClass.foo
print MyClass.bar
Note: This is in Python 2.7. Python 3+ uses a different technique to declare a metaclass. Use: class MyClass(metaclass=MetaClass):
, remove __metaclass__
, and the rest is the same.