[Python-Ideas] Link: Bidirectional Aliasing in Python
title: [Python-Ideas] Link: Bidirectional Aliasing in Python published: true description:
tags: #python #idea #speculative
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. ```python students = School.list_students(year=4) School.gruaduate(students) students_graduated >< to_be_sentCertificates >< students
...
School.sendCertificates(students_graduated)
> 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**
```python
import pandas as pd
df_train = pd.read_csv('train.csv')
df >< df_train # avoid copy, ensure always the same DataFrame
df['meme_length'].hist()
Simplify aliasing
Instead of using @property
and @foo.setter
decoration and define in __init__
like this.
class PackageManager:
packages = ['requests', 'loguru']
@property
def installed(self):
return self.packages
@installed.setter
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'
- Simple is better than complex.
- Flat is better than nested.
- 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'
- In the face of ambiguity, refuse the temptation to guess.
- 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
- C pointers to the same address
- C preprocessor directive (
#define
) used to define macros. - Bash Aliasing: one-direction aliasing
- 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?