Closures in metaclasses

Peter Otten __peter__ at web.de
Thu Jan 21 15:55:09 EST 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" % 
name
...     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