
Hi, The __get__ method of descriptors can be called at the class level (that's how methods work) and in that case instance would be None, but owner will always reference the current class. __set__ can only be called for instances on that class (`Cls.attr = ...` would redefine the class-attribute and not call the descriptor), so instance can never be None and owner is type(instance). Le jeu. 19 oct. 2023 à 09:45, Dom Grigonis <dom.grigonis@gmail.com> a écrit :
On 19 Oct 2023, at 10:27, dn via Python-ideas <python-ideas@python.org> wrote:
On 19/10/2023 19.50, Dom Grigonis wrote:
Thank you, Good information, thank you. Was not aware of __set_name__.
IIRC that was one of the updates/improvements. Thanks to whomsoever...!
The:
instance.__dict__[self.name] = value
may require a bit of thought before it feels comfortable, but it is significantly easier to understand than what we had to do 'before'. I am using `setattr(instance, self.name, value)`. But I see that instance.__dict__ is more appropriate in this case.
Another surprise, and I've assumed you're asking in the context of [Custom] Descriptors, is in how many places/functions Python makes use of a descriptor/descriptor protocol. Yet few of us seem to make use of them in our application code... (YMMV!) I use them more and more.
However, I was more interested, why doesn't __set__ have an `owner` argument, while `__get__` does. I am aware that this is not an issue at all as one can simply do `inst.__class__`, but I am just curious about the reason for inconsistency.
Although, answers that I got were very useful.
DG _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/64MSMP... Code of Conduct: http://python.org/psf/codeofconduct/
-- Antoine Rozo