DEV Community


[Python-Ideas] Link: Bidirectional Aliasing in Python

circleoncircles profile image Nutchanon Ninyawee Updated on ・3 min read

Update Sept, 26 2019: I updated and rewrote this proposal, in a new article here.

I am not good at English wording. I will try my best to communicate the idea.

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

class Human:
    def code(self):
        return "import json"

    # link function name of similar term
    program >< code

# link class name of different languages
link('Human','มนุษย์', '人的', 'человек', '👤')

me = человек()
assert me.program() == me.code() # True

# reassign function
人的.program = lambda x: "import this"
assert มนุษย().program() == me.code() # True

The Good

Alter the variable name throughout a complex process

Easier to wrap your head around. If we can add another meaningful name. Less cognitive load for coder and reader. We rely on fewer comments to keep track of things.

I code what I mean, mean what I code.

students = School.list_students(year=4)
students_graduated >< to_be_sentCertificates >< students



Allow snippet/partial read, without a need to track upper variable changes.

this also may favor a guy who codes on interactive environments like jupyter , explicit not-copy assignment

import pandas as pd

df_train = pd.read_csv('train.csv')
df >< df_train # avoid copy, ensure always the same DataFrame

Simplify aliasing

Instead of using @property and @foo.setter decoration and define in __init__ like this.

class PackageManager:
    packages = ['requests', 'loguru']

    def installed(self):
        return self.packages

    def installed(self, value):
        self.packages = value

    def remove(self):

    def __init__(self)
        self.uninstall = self.remove

We can just

class PackageManager:
    packages = ['requests', 'loguru']
    installed >< packages

    def remove(): 
    uninstall >< remove

Change unintuitive/non-pythonic naming to a better name

Everyone has a different set of vocabulary. Code users may have a hard time to wrap their heads around certain unfamiliar vocabulary created by coders. Linked variables allow them to change to a new name while keeping full compatibility.

from selenium import webdriver

driver = webdriver.Firefox()
webdriver.find_element_by_name >< webdriver.search_element_using_name

elem = driver.search_element_using_name("q")

***Allow unnested naming on part of a specified object

I am not sure that this should be part of link or not. Since not all objects able to assign back etc… The sample would be

house = {'resident_num': 2}
house['resident_num'] >< n_resident
n_resident = 3
print(house['resident_num']) # print 3

Follow ‘The Zen of Python'

  1. Simple is better than complex.
  2. Flat is better than nested.
  3. Namespaces are one honking great idea — let’s do more of those!

The Bad

Novel Idea, No previous implementation to learn from

I don’t see a feature like this in any of programing language. But present in a lot of human-being languages.

Not Follow ‘The Zen of Python'

  1. In the face of ambiguity, refuse the temptation to guess.
  2. There should be one — and preferably only one — obvious way to do it.

The Ugly

Need a System to help individual coders to read/write code

Let say 2 guys from a different country are coding the same script using different a language; English and Russian.
Although they both specify the linked variables explicitly. They will get dizzy soon since they have to look up back and forth a lot when they encounter foreign text. There should be a system to help them select their default language/vocabulary so they can understand code as if it was entirely coded by them.

Similar Existing

  1. C pointers to the same address
  2. C preprocessor directive (#define) used to define macros.
  3. Bash Aliasing: one-direction aliasing
  4. Python Assignment/Chained Assignment: same value in a snap moment.

I would like to know what you guys think. Will it affect your daily coding in a positive/negative way?

Discussion (3)

Editor guide
vedgar profile image
Vedran Čačić

I think in all of those, except maybe the "simplify aliasing" example, '=' does more correct thing than your linking. And it is already supported in Python. For "simplify aliasing", as you said, we already have properties (which are arguably better since you can add validation in .setter).

For example, in your "change non-intuitive naming to a better one", I do it all the time with assignment. And it works according to my expectations. Especially for methods (which is 95% of cases:), since you never really reassign methods after you define them.

circleoncircles profile image
Nutchanon Ninyawee Author

I could not agree more with you. Anyway, I added more clarification with examples here. After go thru that, if you still think that is the case. Then, I think it is actually the case.

vedgar profile image
Vedran Čačić • Edited

Yup, mostly (multilingual coding is an exception, but I strongly believe this is better handled through external tools, not Python language itself). The ones when I don't are the ones where even the link wouldn't help you. For example, you seem to think that "freeing the memory" for an object happens immediately after you del all the names for it.

First, it doesn't have to be, and second, even if it did, it still wouldn't help you much because you can't know, especially with complex frameworks like jupyter, who else "up the chain" and in what place holds a reference to your object. If you really want to explore such ideas, weakref (which is built specifically for that) might be much better choice.