__set_name__ equivalent for instance
Dom Grigonis
dom.grigonis at gmail.com
Thu Nov 16 13:12:19 EST 2023
What I am interested in is a callback.
Preferably just after methods get bound. So in `object.__new__`.
I have done it via metaclass, but it is not ideal as there would be too much overhead.
I think what I am looking for is custom method binding.
Regards,
DG
> On 16 Nov 2023, at 20:02, Dieter Maurer <dieter at handshake.de> wrote:
>
> Dom Grigonis wrote at 2023-11-15 18:44 +0200:
>> So there is a method __set_name__ which is called on class creation.
>>
>> The functionality that I am interested in is not retrieving name, but the fact that it also receives `owner` argument.
>>
>> Thus, allowing simulation of bound class method.
>>
>> I was wandering if there is an equivalent functionality of attribute to receive `instance` argument on instance creation.
>
> As PEP 487 describes, `__set_name__` essentially targets descriptors.
> It is there to inform a descriptor about the name it is used for
> in a class (and the class itself). There is no other (easy) way to allow
> a descriptor to learn about this name.
>
> If a descriptor is accessed via an instance, the descriptor (protocol)
> methods get the instance as parameter.
> Note that descriptors are stored in the class: they must not store
> instance specific information in their attributes.
> Therefore, a method informing an descriptor about instance creation
> would not help: it cannot do anything with it.
More information about the Python-list
mailing list