Closures in metaclasses

Peter Otten __peter__ at
Thu Jan 21 21:55:09 CET 2010

Falcolas wrote:

> I tried overriding __getattr__ and got an error at runtime (the

You can either move __getattr__() into the metaclass or instantiate the 
class. I prefer the latter.

Both approaches in one example:

>>> class Tag:
...     class __metaclass__(type):
...             def __getattr__(self, name): return "<%s> via metaclass" % 
...     def __getattr__(self, name): return "<%s> via class" % name
>>> Tag.yadda
'<yadda> via metaclass'
>>> tag = Tag()
>>> tag.yadda
'<yadda> via class'

More information about the Python-list mailing list