Hello!
I’m new to this forum, so please let me know if I’m asking in the wrong place.
TDLR: implement __rilshift__(self, *args) and such functions for cases when LHS doesn’t exist or doesn’t define __ilshift__. Allow introduction of new variables as the return value of __rilshift__.
Justification (usage example):
I’m working on a Python library that allows building and executing dataflow graphs. These graphs have nodes and edges (big surprise). To create edges between nodes, I’m using the “<<=” operator. So, if you have X and Y as two existing objects, you could say:
X <<= Y
to crate a connection between the two. For more complex scenarios, nodes have named ‘ports’ which can also be connected:
X.input42 <<= Y.output12
Sometimes it’s convenient to give ‘names’ to these edges by assigning temporary variables to them. I call these ‘wires’. The syntax would be:
my_wire = Wire()
my_wire <<= Y.output12
...
X.input42 <<= my_wire
Notice, how I have to declare my_wire up front before the first ‘connection’ of it to output12 of Y. This is not very “pythonic” and in general it would be nice if I didn’t have to do that.
Implementation idea:
My idea would be to introduce a new dunder function:
__rilshift__(self, *args)
This would be called if the LHS of the ‘<<=’ operator doesn’t exist (in which case *args would contain no arguments) or doesn’t implement __ilshift__ (in which case *args would contain a reference to the LHS). Either way, the return value from this function would be bound to the named variable whether it existed prior the call or not.
I don’t believe this change would break any existing code.
Obviously, if this idea is implemented, it would be done for all in-place operators, not just ‘<<=’.
Thank you for reading all the way to the end and please provide feedback!
Thanks,
Andras Tantos