Why should I refer to “names” and “binding” in Python instead of “variables” and “assignment”?

Posted on

Question :

Why should I refer to “names” and “binding” in Python instead of “variables” and “assignment”?

Why should I refer to “names” and “binding” in Python instead of “variables” and “assignment”?
I know this question is a bit general but I really would like to know 🙂

Answer #1:

In C and C++, a variable is a named memory location. The value of the variable is the value stored in that location. Assign to the variable and you modify that value. So the variable is the memory location, not the name for it.

In Python, a variable is a name used to refer to an object. The value of the variable is that object. So far sounds like the same thing. But assign to the variable and you don’t modify the object itself, rather you alter which object the variable refers to. So the variable is the name, not the object.

For this reason, if you’re considering the properties of Python in the abstract, or if you’re talking about multiple languages at once, then it’s useful to use different names for these two different things. To keep things straight you might avoid talking about variables in Python, and refer to what the assignment operator does as “binding” rather than “assignment”.

Note that The Python grammar talks about “assignments” as a kind of statement, not “bindings”. At least some of the Python documentation calls names variables. So in the context of Python alone, it’s not incorrect to do the same. Different definitions for jargon words apply in different contexts.

Answered By: Steve Jessop

Answer #2:

In, for example, C, a variable is a location in memory identified by a specific name. For example, int i; means that there is a 4-byte (usually) variable identified by i. This memory location is allocated regardless of whether a value is assigned to it yet. When C runs i = 1000, it is changing the value stored in the memory location i to 1000.

In python, the memory location and size is irrelevant to the interpreter. The closest python comes to a “variable” in the C sense is a value (e.g. 1000) which exists as an object somewhere in memory, with or without a name attached. Binding it to a name happens by i = 1000. This tells python to create an integer object with a value of 1000, if it does not already exist, and bind to to the name ‘i’. An object can be bound to multiple names quite easily, e.g:

>>> a = []  # Create a new list object and bind it to the name 'a'
>>> b = a   # Get the object bound to the name 'a' and bind it to the name 'b'
>>> a is b  # Are the names 'a' and 'b' bound to the same object?

This explains the difference between the terms, but as long as you understand the difference it doesn’t really matter which you use. Unless you’re pedantic.

Answered By: aquavitae

Answer #3:

I’m not sure the name/binding description is the easiest to understand, for example I’ve always been confused by it even if I’ve a somewhat accurate understanding of how Python (and cpython in particular) works.

The simplest way to describe how Python works if you’re coming from a C background is to understand that all variables in Python are indeed pointers to objects and for example that a list object is indeed an array of pointers to values. After a = b both a and b are pointing to the same object.

There are a couple of tricky parts where this simple model of Python semantic seems to fail, for example with list augmented operator += but for that it’s important to note that a += b in Python is not the same as a = a + b but it’s a special increment operation (that can also be defined for user types with the __iadd__ method; a += b is indeed a = a.__iadd__(b)).

Another important thing to understand is that while in Python all variables are indeed pointers still there is no pointer concept. In other words you cannot pass a “pointer to a variable” to a function so that the function can change the variable: what in C++ is defined by

void increment(int &x) {
    x += 1;

or in C by

void increment(int *x) {
    *x += 1;

in Python cannot be defined because there’s no way to pass “a variable”, you can only pass “values”. The only way to pass a generic writable place in Python is to use a callback closure.

Answered By: 6502

Answer #4:

who said you should? Unless you are discussing issues that are directly related to name binding operations; it is perfectly fine to talk about variables and assignments in Python as in any other language. Naturally the precise meaning is different in different programming languages.

If you are debugging an issue connected with “Naming and binding” then use this terminology because Python language reference uses it: to be as specific and precise as possible, to help resolve the problem by avoiding unnecessary ambiguity.

On the other hand, if you want to know what is the difference between variables in C and Python then these pictures might help.

Answered By: jfs

Answer #5:

I would say that the distinction is significant because of several of the differences between C and Python:

  1. Duck typing: a C variable is always an instance of a given type – in Python it isn’t the type that a name refers to can change.

  2. Shallow copies – Try the following:

    >>> a = [4, 5, 6]
    >>> b = a
    >>> b[1] = 0
    >>> a
    [4, 0, 6]
    >>> b = 3
    >>> a
    [4, 0, 6]

This makes sense as a and b are both names that spend some of the time bound to a list instance rather than being separate variables.

Answered By: Steve Barnes

Leave a Reply

Your email address will not be published. Required fields are marked *