__set__ method is not called for class attribute access
ericsnowcurrently at gmail.com
Wed Aug 10 18:27:20 CEST 2011
On Wed, Aug 10, 2011 at 8:33 AM, Fuzzyman <fuzzyman at gmail.com> wrote:
> 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).
Keep in mind that you can still do something like this:
class X(Base, metaclass=XMeta):
"Do your stuff."
They you would put your descriptor hacking in XMeta and still take
advantage of the original metaclass.
> Michael Foord
More information about the Python-list