23

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 :)

6
  • 1
    Because.. It's just what it is.. A name is a name, and it's bound somewhere..
    – aIKid
    Dec 20, 2013 at 8:59
  • 5
    You "should" only do this if you care about pedants harassing you about it. What's important is not how you refer to them, but whether you understand how they work in Python.
    – BrenBarn
    Dec 20, 2013 at 9:06
  • 1
    Who told you that you should do that? Where did that come from? I'm always using "variables" and "assignment".
    – freakish
    Dec 20, 2013 at 9:10
  • 2
    I usually talk about names referring to things when I'm explaining the semantics, and whatever terms I feel like at the moment otherwise. Dec 20, 2013 at 9:13
  • Well, in Python, you do not put a value in a variable, you bind a value to a label.. I think ^^. The distinction is not that useful, since with knowledge of references in other languages and some try / fail / redo using mutable default values for functions arguments, you get the most of it. But those are just my thoughts .. idk whether they are correct or not Dec 20, 2013 at 9:16

5 Answers 5

34

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.

4
  • 1
    a code sample showing the behavioral difference between variables and names would be a nice to have. Dec 31, 2016 at 22:29
  • 5
    @AlexanderMills: here are pictures that illustrate the difference
    – jfs
    Feb 25, 2017 at 18:41
  • 5
    The link in jfs's comment is dead, but here's an archived version.
    – PM 2Ring
    Oct 17, 2019 at 8:25
  • +1 for variable = named storage location and Python assignment = binding. You did not mention it explicitly, so I would like to add this: C assignment modifies the store (the function Location → Content), while Python assignment modifies the environment (the function Name → Location).
    – Géry Ogam
    Sep 29, 2021 at 11:33
4

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?
True

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.

3
  • 1
    a = [1], and then a[0] = []. To what name is bound the second list?
    – 6502
    Dec 20, 2013 at 9:26
  • @6502 My answer is a bit of a simplification of what actually goes on behind the scenes. Binding happens in python in other ways than just to names. In CPython, there is actually C-style pointer assignment going on here (although technically that's happening for any name binding).
    – aquavitae
    Dec 20, 2013 at 9:40
  • 2
    "The closest python comes to a "variable" in the C sense" -- also the closest that C++ comes to a variable in the Python sense is probably a reference. C++ references cannot be reseated to refer to a different object, but they do have the property that via references, a single object can be bound to multiple names simultaneously. Dec 20, 2013 at 9:49
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.

1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.