__set__ method is not called for class attribute access

Fuzzyman fuzzyman at gmail.com
Wed Aug 10 12:40:03 EDT 2011

On Aug 10, 5:27 pm, Eric Snow <ericsnowcurren... at gmail.com> wrote:
> On Wed, Aug 10, 2011 at 8:33 AM, Fuzzyman <fuzzy... 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.

Yeah, the way round the "more than one metaclass problem" is to have
your new metaclass inherit from the first one. That's not a general
purpose solution though.


More information about the Python-list mailing list