I know I can write code like:
class A :
def __getattr__ (self, name) :
return name
to trap access to undefined attributes on an
Yes you can. Metaclasses are the answer.
class MyMetaclass(type):
def __getattr__(cls, name):
return "cls.%s" % name
class A :
__metaclass__ = MyMetaclass
def __getattr__ (self, name) :
return name
print A().ATTR
print A.ATTR
will output
ATTR
cls.ATTR
You can use a metaclass:
In [1]: class meta(type):
...: def __getattr__(self, name):
...: return name
...:
...:
In [2]: class A(object):
...: __metaclass__ = meta
...: def __getattr__(self, name):
...: return name
...:
...:
In [3]: A().attr
Out[3]: 'attr'
In [4]: A.attr
Out[4]: 'attr'