__set__ method is not called for class attribute access

Eric Snow 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 XMeta(Base.__class__):
    "Customize here"

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.

-eric

>
> Michael Foord
> --
> http://voidspace.org.uk/
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list