[Python-Dev] PEP487: Simpler customization of class creation
Martin Teichmann
lkb.teichmann at gmail.com
Sun Jul 24 03:31:38 EDT 2016
Hi list, Hi Nick,
Sorry for my delayed response, it is summer here...
> However, phrasing it that way suggest that it's possible we *did* miss
> something in the PEP: we haven't specified whether or not __set_name__
> should be called when someone does someone does "cls.attr = descr".
> Given the name, I think we *should* call it in that case, and then the
> semantics during class creation are approximately what would happen if
> we actually built up the class attributes as:
>
> for attr, value in cls_ns.items():
> setattr(cls, attr, value)
That's a very good point and actually easy to solve: we would just
need to override type.__setattr__ to do so. Actually, it is already
overridden, so we just need to add code to type.__setattr__ to also
call __set_name__.
One could be of the other standpoint that in your above example it's
the duty of the caller of setattr to also call __set_name__. It would
be pretty easy to add a line in the loop that also calls
__set_owner__.
Greetings
Martin
More information about the Python-Dev
mailing list