On Mon, Sep 23, 2019 at 10:42 AM Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:
On Sep 22, 2019, at 02:08, Nutchanon Ninyawee <me@nutchanon.org> wrote:
Link is a language feature that allows multiple variable names to always refer to the same underlying object define in a namespace. For now, if the variable a link with b. I will denote as a >< b or link('a', 'b')
a = 2 a >< alpha # link 'a' with 'alpha' b = 3 print(b**alpha) # print 9 alpha = 3 print(b**a) # print 27
How would this work? I can think of a few options, but they all either badly slow down every variable use, don’t work with locals, or require >< to be handled at compile time. Do you have at least a sketch of how this works?
Since there's nothing in the OP's article about implementation, I'm going to assume that the OP hasn't actually planned any, and is just looking at syntax and semantics. So there's plenty of flexibility to discuss potential implementations. Currently, Python has a default set of rules for how to interpret the meaning of a bare name, but you can alter that with "global" and "nonlocal" declarations. Aliasing could be handled exactly the same way: at compile time, interpret this name as meaning the same as that name. It would be more Pythonic to use a keyword rather than the "><" glyph (even while typing it for that example, my fingers autocorrected to "<>", which would be syntactically valid but usually meaningless - a dangerous trap), so let's suppose that we can use the word "alias" for this. The trouble is that the "alias-ness" of it needs to be able to be stored in a namespace somehow. The class example implies that "program" is an alias for "code" in some way that means that can be stored in the class's dict, and doesn't interfere with descriptor protocol for the function (which, I think, implies that it cannot itself be a descriptor). The same applies to monkeypatching Selenium (in the given example, I honestly think the original name is better, but that's just a matter of the example); the alias has to be able to be stored in the instance's dict, and then looked up. In some cases, it may be possible to translate "x.y >< z" into something akin to "type(x).y = property(lambda: z, lambda v: z=v)" (yes, I know, a lambda function can't assign like that, but this is compiler magic so it can do what it likes). Unfortunately that would apply to ALL instances of that type - probably not a problem in the OP's Selenium example, but a fairly major problem when working with a generic class (ie where type(x) is type). Not sure where this leads us, though, as instance properties have been proposed and rejected before. ChrisA