variable vs. object

Nagy László Zsolt gandalf at shopzeus.com
Mon Nov 30 03:09:32 EST 2015


> a=10
>
> 'a' is an integer. Is it an object too?
In Python, objects have an identity. When you do "a=10" then you *bind*
the object to the name *a*. By "variable", the documentation refers to a
name that was bound to an object. This is different from many other low
level languages. For example: in C, you can do

a=10;

And it will "set the value of the variable 'a' to 10". In other words:
"a" is not just a name, it refers to a certain location in memory where
an integer value is stored. And in C, it is not an object but a value of
a built-in type.

In contrast, this is what happens in Python:

  * An "Integer" object is constructed with the value 10.
  * This object is then bound to the name "a".

The key point is that "variables" are just names that may reference to
objects. There is a distinction between names and objects. Strictly
speaking, you cannot "set the value of a variable", because variables do
not hold values. They just refer to objects. In Python, you can only
"create an object", or "change the state of an object", and "bind a name
to an object".



More information about the Python-list mailing list