8

According to what I know, a variable in Python is a name that refers to a value stored in the computer memory, like a label on a box.

but in other programming languages a variable is a location, in memory, where values are stored, and it's like a box.

  • Why does that difference exist?

  • Python Documentation doesn't tell us what a variable is, so how do Python book authors know what it is?

  • 3
    There is no such difference. A Basic variable might be called `A` and hold values such as 1, 2, 3... depending what you store into it. A Python variable can also be called `A` and hold 1, 2, 3... depending what you store into it. Does your text really make that distinction between Python and all other languages? – Kilian Foth Apr 05 '16 at 13:09
  • 7
    This is not a matter of Python vs. all other languages. Countless other popular languages are very much like Python in this respect, with only some slight differences. Examples include Java, virtually all other JVM languages including e.g. Groovy and Scala, Ruby, C#, other .NET languages like VB.NET, etc. –  Apr 05 '16 at 13:24
  • Each language treats "variable" slightly differently. It all depends on language's semantics how variables are handled and implemented. – Euphoric Apr 05 '16 at 13:27
  • 1
    All I see there is the same concept expressed two different ways. – Blrfl Apr 05 '16 at 13:28
  • The first is just a name that refers to a value, but the second is a container that contains a value. – Mahmood Muhammad Nageeb Apr 05 '16 at 13:32
  • 1
    This should not be closed, it is a valid question with a good answer. – Gringo Suave Nov 07 '19 at 17:10

1 Answers1

18

This is not a Python vs Other Languages distinction - it's actually Value Types vs Reference Types distinction. Python uses reference types, and while many modern languages also tend to use reference types, it's common to compare Python(or any language, actually) to C/C++, which use value types.

(I'm simplifying things a lot here - there are languages that support both reference and value types, languages that use value types usually have pointers, which are the value-type version of reference types, and some languages that use reference types use value types for primitives)

With reference types - like what Python uses - the variable refers to the "box" in the memory. This means that multiple variables may refer to the same box:

class Foo:
    x = 1

foo = Foo()
bar = foo
bar.x = 10
print(foo.x)  # prints 10

When we changed bar.x, foo.x was changed as well. This is because foo and bar are both labels to the same box.

Compare with this C code:

#include<stdio.h>

struct Foo {
    int x;
};

int main(int ARGC, char** ARGV) {
    struct Foo foo, bar;
    foo.x = 0;
    bar = foo;
    bar.x = 10;
    printf("%d\n", foo.x); // prints 0
    return 0;
}

C uses value types, so foo and bar are not "labels" - they are the boxes themselves. That's why when we change bar.x, foo.x does not change - they are different boxes, and bar = foo does not make bar refer to the same box as foo like it did in Python - instead, it copies the content of the foo box into the bar box.

Idan Arye
  • 12,032
  • 31
  • 40
  • 2
    Python uses reference *semantics* in its *specification*, but the most popular Python implementations often use *value* semantics in their *implementation* for performance reasons. (In cases, where such optimizations are legal, of course.) – Jörg W Mittag Apr 05 '16 at 13:54
  • 1
    @JörgWMittag CPython is *by far* the most popular implementation, and it does no such thing. PyPy does, Jython may, but they're so far behind in popularity that I wouldn't count them as "popular". Perhaps you're confusing Python with other languages that are all-reference-types such as Ruby? –  Apr 05 '16 at 14:44
  • @delnan: Uh, you're right. It's such an obvious optimization, I always assumed it would be done in CPython, and at some point, I simply forgot that I had originally assumed it and assumed I had actually checked it :-D Pyston is currently implementing it, PyPy does, as you mentioned. CPython will probably never do it, Guido van Rossum himself left a rather firm comment on the GitHub issue for a tagged pointer representation for Pyston to that effect, effectively warning them against even trying it. You're also right that *all* Ruby implementations do it, even MRI, probably the simplest, slowest – Jörg W Mittag Apr 05 '16 at 15:31
  • … dumbest, least optimizing language execution engine ever created :-D – Jörg W Mittag Apr 05 '16 at 15:32
  • 1
    "to C/C++, which use value types." C/C++ has value types and reference types. The "type" in Python is semantically equivalent to C/C++ pointers. – user102008 Apr 12 '16 at 06:01