• __set_name__ equivalent for instance

    From Dom Grigonis@21:1/5 to All on Wed Nov 15 18:44:11 2023
    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.

    Regards,
    DG

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dom Grigonis@21:1/5 to All on Thu Nov 16 20:12:19 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@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Dom Grigonis on Thu Nov 16 20:00:46 2023
    Dom Grigonis wrote at 2023-11-16 20:12 +0200:
    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.

    Methods are not bound during instance creation, they are bound during
    access.

    You can use descriptors to implement "custom method binding".

    Descriptors are defined on the class (and do not behave as
    descriptors when defined on instances).
    Thus, you would need a metaclass or `__inist_subclass__` is you
    want your "custom method binding" globally.


    For many methods (but usually not the `__...__` methods),
    you can take over the binding in `__new__` or `__init__`
    and populate the instance with prebound methods.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Dom Grigonis on Thu Nov 16 19:02:15 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dom Grigonis@21:1/5 to All on Thu Nov 16 21:11:18 2023
    On 16 Nov 2023, at 21:00, Dieter Maurer <dieter@handshake.de> wrote:

    Dom Grigonis wrote at 2023-11-16 20:12 +0200:
    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.

    Methods are not bound during instance creation, they are bound during
    access.

    Good to know. What is the criteria for binding then? Does it check if its type is `vanilla` function? If yes, is there any way to simulate it for arbitrary object?

    I have tried inheriting from function type, but not allowed.

    You can use descriptors to implement "custom method binding".

    Descriptors are defined on the class (and do not behave as
    descriptors when defined on instances).
    Thus, you would need a metaclass or `__inist_subclass__` is you
    want your "custom method binding" globally.

    Yes, I have tried that. This works well. But maybe will be useful for another case.

    Currently, the focus is decorators that can be used on arbitrary methods. Needing to inherit and add metaclasses whenever I want to decorate is not an option.

    I think I will continue with descriptor approach and am slowly finding route to get where I need to, but still exploring options.

    Regards,
    DG

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dom Grigonis@21:1/5 to All on Thu Nov 16 21:35:31 2023
    Thank you.

    On 16 Nov 2023, at 21:30, Dieter Maurer <dieter@handshake.de> wrote:

    Dom Grigonis wrote at 2023-11-16 21:11 +0200:
    ...
    On 16 Nov 2023, at 21:00, Dieter Maurer <dieter@handshake.de> wrote:
    ...
    Methods are not bound during instance creation, they are bound during
    access.

    Good to know. What is the criteria for binding then? Does it check if its type is `vanilla` function? If yes, is there any way to simulate it for arbitrary object?

    Attribute access (including method binding) is documented in the
    language reference.

    Functions and descriptors accessed via an instance but found in a class (i.e. not directly in the instance) are handled specially;
    functions are bound. Other objects are returned as is.

    `__getattribute__` can be used to take over control over the attribute access.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Dom Grigonis on Thu Nov 16 20:30:28 2023
    Dom Grigonis wrote at 2023-11-16 21:11 +0200:
    ...
    On 16 Nov 2023, at 21:00, Dieter Maurer <dieter@handshake.de> wrote:
    ...
    Methods are not bound during instance creation, they are bound during
    access.

    Good to know. What is the criteria for binding then? Does it check if its type is `vanilla` function? If yes, is there any way to simulate it for arbitrary object?

    Attribute access (including method binding) is documented in the
    language reference.

    Functions and descriptors accessed via an instance but found in a class (i.e. not directly in the instance) are handled specially;
    functions are bound. Other objects are returned as is.

    `__getattribute__` can be used to take over control over the attribute
    access.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)