What is an object in Python. Why id (a) = = id (b) = = id (1)?

I am detailing the question.
The Python books say that everything in Python is an object.

We take a traditional approach to learning / teaching programming languages. Languages Pascal, C, C++, they have the concept of "variable name" (identifier). A value is associated/assigned to the variable name. The value is stored in memory. A variable name is a way (in the source code of a program) to access a memory location to get the value that is stored there.

I don't know whether the statement is true (for compiled languages): For variables, for example, an integer type, at the time of program execution on the computer, the processor already works with the addresses of memory cells where the values of variables are stored. I.e., not such that somewhere at some address the name of the variable is stored and it is associated with the address of the cell in which the value is stored.

Now let's move on to Python. In this language, everything is an object, even the definition of a function.
The object variable, the object value, and etc. At different times, different other objects can relate to the same object.

a = 1
print(a)
a = "Hello"
print(a)

But then how to understand the action of the function id()? By definition of the standard it returns the" identify " of the specified object. Moreover, " returns an integer that is guaranteed to be unique" and constant for the object for the duration of its existence. "

Then why

a = 1
b = 1
print(id(a) == id(b) == id(1))  # True
print("Why?")

After all, a, b, 1 are different objects?
In my opinion, the question turned out...

Python is used 3.6


I've heard about values from -5 to 256. Takes place in

a = 1000000
b = 1000000
print(id(a) == id(b))  # True
print("Why?")

At the expense of the fact that it is enough to " know about names, etc.". For me, it is not enough, so I ask the respected pros. In Python, everything is an object. No variable names. Hence my question.

Author: jfs, 2017-05-05

3 answers

In python, the interpreter is optimized so that small integers are represented by a single object - this is done in order to improve performance. For large numbers, this is no longer true.

Mark Lutz-Learning Python, 4th Edition, 2011, p. 204:

>>> X = 42 
>>> Y = 42  # Должно получиться два разных объекта 
>>> X == Y
True

>>> X is Y  # Тот же самый объект: кэширование в действии!
True

In this example, the variables X and Y must be equal ( = = , the same thing value), but not equivalent to (is, the same object), because two different literals were executed expressions. However, because of the, that small integers and strings are cached and reused, the is operator reports that the variables refer to the same object.

 8
Author: Xander, 2017-05-05 17:34:34

At different times, different other objects may relate to the same object.

You wanted to say: at different times, the same name can refer to different objects

The Python model is simple - you don't need to know what an address, memory location, or pointer is. It is enough to know only about the names and objects to which they are bound.

Here are pictures that explain the difference between variables in C like languages and names in Python. Labels and balls are sufficient to explain any behavior at the Python level - at first it is hard to believe that such a simple model covers all extreme cases.

After all, a, b, 1 are different objects?

You only have one object (unit). Small integers are cached in CPython. Different names a, b refer to the same object: a is b (id(a) == id(b)). 1 this is a constant in the source code (think of it as calling a constructor for an object). Learn more about int objects: Assignment in Python.

A = 1000000; b = 1000000; print (id(a) == id(b)) # True

To check whether the names refer to the same objects, use a is b (here it is equivalent to id(a) == id(b), but id can be overridden in general).

The result can be True (depends on how the Python code was compiled into the bytecode in the selected implementation), but does not have to be True:

>>> a = 1000000
>>> b = 1000000
>>> print (a is b) 
False

In this case,, each expression is compiled separately, a and b refer to different objects. Compare:

>>> def f():
...     a = 1000000
...     b = 1000000
...     print(a is b)

>>> f()
True

The function code is compiled as a whole, so a is b can (but does not have to) be True. One or more of these objects may depend on the Python implementation (CPython, Pypy, Jython, etc) and even the specific implementation version. The actual code should not depend on such implementation details. As explicitly stated in the link: do not use identity verification for number comparisons (different objects can have the same value). Use a == b to find out if the numbers are equal.

Python is all object. No variable names. Hence my question.

Of course there are names in Python. a and b are the names in your question.

The fact that the Python code in different representations can be manipulated as a simple object (pass it to a function as a parameter, return it from a function, call methods, etc.) does not negate the presence of names.

Examples:

Also, for manipulating Python code, it may be useful to know about compile (), eval (), exec () built-in functions, dis, uncompyle6, inspect modules. For example, inspect.signature() allows you to manipulate the function description as an object. The already mentioned link shows how the very memory occupied by an object in the CPython implementation can also be manipulated using ctypes like a regular Python object (the example modifies an int object that is immutable at the Python level).

 14
Author: jfs, 2020-04-08 19:15:49
  1. variables store only references to the object
  2. operator = assigns the variable reference
  3. primitive types (literals: numbers, strings) with the same value are represented by a single object
 3
Author: Dmitry Ponyatov, 2017-05-05 16:44:49