Apr-27-2019, 04:31 PM
(This post was last modified: Apr-27-2019, 04:31 PM by Gribouillis.)
Nwb Wrote:But in Python, assigning to an integer variable would mean allocating new memory for a new objectThis is not true. Assigning to an integer variable simply increases the reference count of an existing (python) integer instance.
Your reasoning works for certain operations such as augmented assignments
x += 1In python this augmented assignment creates a new PyInt_Object because the intitial object may have other references pointing at it. Variables, better called names in python, are only references to shared objects.
The design of python is excellent, which explains the success of the language. The best thing to do if you want to understand it is to write a few C extension modules using the C api.