-
Notifications
You must be signed in to change notification settings - Fork 172
LPython Semantics
LPython is designed to compile such a subset of Python so that we can modify the Python's semantics to always copy things over for a = b
(no reference counting), and still be equivalent to Python. This is done for performance reasons. The idea is that LPython will not compile your code if it does not follow the restricted subset. If you want reference counting, then one must ask for it explicitly.
>>> src = [] # fine
>>> dest = src # fine
>>> dest.append(1) # fine
>>> src # error --- LPython and CPython would differ here, so we must disallow at compile time
If you want reference counting, then we could do something like:
>>> src = rcp([]) # fine
>>> dest = rcp(src) # fine
>>> dest.append(1) # fine
>>> src # fine
Later, we can add ASR->ASR optimizations that turn this:
src = []
dest = src # deep copy
dest.append(1)
print(dest)
To this:
src = []
src.append(1)
print(src)
We can also use deepcopy
in both CPython and LPython as follows:
>>> from copy import deepcopy
>>> src = [] # fine
>>> dest = deepcopy(src) # fine
>>> dest.append(1) # fine
>>> src # fine
So if you want to use both src
and dest
at the same time in LPython, you have two options:
- Use
deepcopy
- Use
rcp
In both cases you have to do this explicitly. Without being explicit, you can only use one at the time, so that the LPython's default "copy" semantics and the CPython's default "rcp" semantics are equivalent.