Closures in metaclasses

Arnaud Delobelle arnodel at googlemail.com
Thu Jan 21 16:06:30 EST 2010


Falcolas <garrickp at gmail.com> writes:

> On Jan 21, 12:10 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
[...]
>> Or you could override __getattr__
>>
>> --
>> Arnaud
>
> I tried overriding __getattr__ and got an error at runtime (the
> instance did not have xyz key, etc), and the Tag dict is not
> modifiable (granted, I tried Tag.__dict__[tag] = spam, not setattr()).

Yes that's because __getattr__ works on instances, not on the class
itself.  To achieve this, you'd have to override the *metaclass*'s
__getattr__ method.

But are you sure that you want lots of staticmethods?  Why not
instanciate your class Tag instead and have normal methods to generate
tags?  This would have the added benefit that you could set some options
in the __init__ method of Tag to customize its behaviour.

If you have a class which only contains static methods, you'd be better
off with a module which contains good old functions.

-- 
Arnaud



More information about the Python-list mailing list