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

BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70
  • 1
    Because.. It's just what it is.. A name is a name, and it's bound somewhere.. – aIKid Dec 20 '13 at 08: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 '13 at 09: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 '13 at 09: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. – user2357112 Dec 20 '13 at 09: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 – BoJack Horseman Dec 20 '13 at 09:16
  • @IbrahimApachi: actually, if you're using this approach, it can be helpful to talk about binding the label to the value, instead of binding the value to the label. So `a = 1` is binding the name `a` to the object `1`. `b = a` binds an additional name, `b`, to whatever object the name `a` is bound to, and so on. It forces thinking about objects as "the actual thing", and it helps draw a distinction from C-style variable assignment, in which it very definitely is the value `1` assigned to the object named `a`, and *not* the other way around. – Steve Jessop Jul 28 '17 at 09:35

5 Answers5

33

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.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 1
    a code sample showing the behavioral difference between variables and names would be a nice to have. – Alexander Mills Dec 31 '16 at 22:29
  • 5
    @AlexanderMills: here are [pictures that illustrate the difference](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) – jfs Feb 25 '17 at 18:41
  • 5
    The link in jfs's comment is dead, but here's an [archived version](https://web.archive.org/web/20180411011411/http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). – PM 2Ring Oct 17 '19 at 08: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 '21 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.

aquavitae
  • 17,414
  • 11
  • 63
  • 106
  • 1
    `a = [1]`, and then `a[0] = []`. To what name is bound the second list? – 6502 Dec 20 '13 at 09: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 '13 at 09: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. – Steve Jessop Dec 20 '13 at 09: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.

6502
  • 112,025
  • 15
  • 165
  • 265
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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73