• extend behaviour of assignment operator

    From Guenther Sohler@3:633/280.2 to All on Tue Jan 9 18:14:31 2024
    Hi,

    when i run this code

    a = cube([10,1,1])
    b = a

    i'd like to extend the behaviour of the assignment operator
    a shall not only contain the cube, but the cube shall also know which
    variable name it
    was assigned to, lately. I'd like to use that for improved user interaction.

    effective code should be:

    a=cube([10,1,1])
    a.name='a'

    b=a
    b.name='b' # i am aware that a.name also changes


    can decorators also be used with assignment operators ?

    thank you for your hints

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Dieter Maurer@3:633/280.2 to All on Thu Jan 11 05:44:31 2024
    Guenther Sohler wrote at 2024-1-9 08:14 +0100:
    when i run this code

    a = cube([10,1,1])
    b = a

    i'd like to extend the behaviour of the assignment operator
    a shall not only contain the cube, but the cube shall also know which >variable name it
    was assigned to, lately. I'd like to use that for improved user interaction.

    `Acquisition` (--> `PyPI`) implements something similar.

    It does not work for variables -- but for attribute access.
    Look at the following code:

    ```
    from Acquisition import Implicit

    class AccessAwareContainer(Implicit):
    ...

    class AccessAwareContent(Implicit):
    ...

    container = AccessAwareContainer()
    container.content = AccessAwareContent()
    ```

    When you now assign `content = container.content`, then
    `content` knows that it has been accessed via `container`.


    If fact `content` is not a true `AccessAwareContent` instance
    but a wrapper proxy for it. It mostly behaves like an
    `AccessAwareContent` object but has additional information
    (e.g. it knows the access parent).


    It works via a special `__getattribute__` method, essentially
    implemented by:

    ```
    def __getattribute__(self, k):
    v = super().__getattribute__(k)
    return v.__of__(self) if hasattr(v, "__of__") else v
    ```

    Your use case could be implemented similarly (again not for variables
    and all objects, but for special classes (and maybe special objects)).

    Your `__getattribute__` could look like:
    ```
    def __getattribute__(self, k):
    v = super().__getattribute__(k)
    try:
    v.name = k
    except TypeError: pass
    return v
    ```

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Thu Jan 11 06:40:49 2024
    Guenther Sohler <guenther.sohler@gmail.com> writes:
    i'd like to extend the behaviour of the assignment operator

    The following attempt is just a proof of concept, as it will
    not handle properly all possible code. To do something similar
    in a more general way, use "import ast", parse the code into
    an AST and then search the AST for assignements and insert
    code to change the name attribute after them.

    import re

    class thing: pass # all things pass

    code = r'''
    a = thing()
    print( a.name )
    b = a
    print( b.name )
    '''

    code = \
    re.sub\
    ( r'((?:^|\n)(\s*)([\w_]+)\s*=.*(\n|$))', r'\1\2\3.name="\3"\n', code )
    exec( code )


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From avi.e.gross@gmail.com@3:633/280.2 to All on Sat Jan 20 11:32:25 2024
    Guenther,

    It is best not to suggest a drastic fix for a more limited problem.

    As a general rule, many programming languages only have a pointer concept
    even vaguely along the lines you want for garbage collection purposes. An
    area of memory may have stored alongside it how many other things point at
    it but not which ones. As long as it is decremented when a pointer leaves,
    it works.

    If you want to design objects that can store additional info when invoked properly, go for it. No change to python would be needed. In your example,
    you could create an object initialized by cube([10,1,1], "a") which now
    might remember that something called "a" once pointed at it. But you then
    have to figure out how to ensure than when "a" is deleted or reset or goes
    out of the current environment, that things are properly updated.

    I am not so sure how easy it would be to change the language so it pays attention to what it is giving a pointer too and then goes and tells ...



    -----Original Message-----
    From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Guenther Sohler via Python-list
    Sent: Tuesday, January 9, 2024 2:15 AM
    To: python-list@python.org
    Subject: extend behaviour of assignment operator

    Hi,

    when i run this code

    a = cube([10,1,1])
    b = a

    i'd like to extend the behaviour of the assignment operator
    a shall not only contain the cube, but the cube shall also know which
    variable name it
    was assigned to, lately. I'd like to use that for improved user interaction.

    effective code should be:

    a=cube([10,1,1])
    a.name='a'

    b=a
    b.name='b' # i am aware that a.name also changes


    can decorators also be used with assignment operators ?

    thank you for your hints
    --
    https://mail.python.org/mailman/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)