__set__ method is not called for class attribute access

Fuzzyman fuzzyman at gmail.com
Wed Aug 10 16:33:09 CEST 2011

On Aug 5, 12:29 pm, Ryan <heni... at yahoo.com> wrote:
> In the context of descriptors, the __set__ method is not called for
> class attribute access. __set__ is only
> called to set the attribute on an instance instance of the owner class
> to a new value, value. WHY?

It's an unfortunate asymmetry in the descriptor protocol. You can
write "class descriptors" with behaviour on get, but not on set.

As others point out, metaclasses are an ugly way round this
(particularly given that *basically* a class can only have one
metaclass - so if you're inheriting from a class that already has a
custom metaclass you can't use this technique).

Michael Foord

More information about the Python-list mailing list