Python Names

Python doesn’t have variables, instead they have names. There is no concept of variables in Python.

Note

The notion we have of a variable is something which stores value and it can be changed through out the program life cycle. There can be different implementation of in different programming languages. For instance in C, a variable owns a memory location and stores a value in it. If a new value is reassigned, the memory location remains the same but the value is overwritten on the same location.

Understanding Names

Considering the following statement,

a = 1

Following things happen when this statement executes.

  1. A PyObject is created. This PyObject can’t be accessed by user program. It is internal to CPython implementation.
  2. typecode is set as integer.
  3. value is set as 1.
  4. ref count is increased by 1.
  5. name is a is bounded to this PyObject.

pyobject bounded to a python name

Tip

sys.getrefcount method returns reference count to a PyObject.

References

  1. https://realpython.com/pointers-in-python/