Mathieu Larose Projects Hire Me

Understanding Python Variables

December 2015

In this article, you will learn the behavior of variables in Python and, in particular, the semantics of the assignment operator (=).

Variables or names?

Just before we start, I would like to tell you that what you call a variable in other programming languages is called a name in Python.

Of course not everyone agrees with that, as you can see in this thread. But the official documentation and almost all articles written on the execution model of Python use the word name, so I'll use it here as well.

Name binding

Python has name binding. Name binding is the association between a name and an object (value). So in Python, we bind (or attach) a name to an object.

One way to bind a name to an object is to use the assignment operator (=). For example, the following code binds the name x to the object 1 (an integer whose value is 1):

>>> x = 1

After this assignment, the state of the Python interpreter is:

x ---> 1

It has one name (x) and one object (1).

When you enter a name in Python, it gives you back the object bound to it.

>>> x
>>> 1

This means: give me the object bound to the name x. Python answers with 1.

Let's bind another name:

>>> y = 2

Python creates a name y and an object 2 and binds y to 2.

State:

x ---> 1
y ---> 2

It has two names (x and y) and two objects (1 and 2).

So far we used objects (1 and 2) on the right-hand side of the = operator (x = 1 and y = 2). Here is what happens when we use a name on the right-hand side of the = operator:

>>> z = x

This means: bind the name z to the object bound to the name x.

State:

x ---> 1 <--- z
y ---> 2

It has three names and two objects.

Python created a name z and bound it to the object 1, the object bound to x. Note that Python didn't create or copy the object 1, it only introduced a new name for that object. So both x and z are bound to the object 1.

Remember, a name can only be bound to an object, not to a name.

Next:

>>> x = y

This means: bind the name x to the object bound to the name y. Since x was already bound to the object 1, we say that we rebound x.

State:

       1 <--- z
y ---> 2 <--- x

It has three names and two objects.

x is now bound to the object 2.

Note that x = y doesn't involve z, even though we previously did z = x. So z is still bound to the object 1.

Modifying an object

So far we used immutable objects (integers). Let's see an example with a list.

>>> a = [1, 2]
>>> b = a

State:

a ---> [1, 2] <--- b

It has two names (a and b) and one object ([1, 2]).

Let's execute this line:

>>> a.append(3)

State:

a ---> [1, 2, 3] <--- b

a.append(3) doesn't bind (or rebind) a name, it simply modifies (mutates) an object. It modifies [1, 2], the object bound to the name a. So the names a and b are still bound to the same object, but that object has been modified. It has now three elements:

>>> a
>>> [1, 2, 3]
>>> b
>>> [1, 2, 3]

Now what happens when we execute the following code?

b = [10]

Is the name a bound to the object [10]? You can find the answer on Python Tutor.

Need a search bar for your website? Make it easy for visitors to find what they need with Lixia Search.