Assignment Considered Harmful

We do a disservice to the understanding of Python when we refer to Python identifiers as "variables" and binding as "assignment." The words "variable" and "assignment" are particularly loaded with meanings from languages other than Python. And those meanings do not reflect the dynamic nature of Python.

For most languages, "variable" refers to a named location in memory and the act of assignment refers to populating that memory location with data. Subsequent assignment of a variable to another variable means to allocate yet another named location in memory and then copy data from one location to another. Data associates with only one variable name in this scheme.

In Python, however, objects are created in memory without the necessity of a name. In order to reference objects (and prevent them being garbage collected), we bind identifiers to objects. A single object may be bound to multiple identifiers (in multiple namespaces, even). And rebinding does not create new copies of objects implicitly.

The hazard in refering to identifiers as "variables" is the suggestion that objects are being "stored" in identifiers. And the pitfall in refering to binding as assignment is the suggestion that objects are being copied.

The use of the words "variable" and "assignment" when referring to identifiers and binding in Python is widespread, even penetrating the official Python documentation and the writings of the BDFL. It would promote a better understanding for newcomers to Python, however, if that unfortunate habit would be left behind.

social