Closures in metaclasses

Falcolas garrickp at gmail.com
Thu Jan 21 16:01:33 EST 2010


On Jan 21, 1:55 pm, Peter Otten <__pete... at web.de> wrote:
> 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'

Very nice, thanks!



More information about the Python-list mailing list