Avatar ·

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

📁 python, пример, ии, книга, работа

"

I will elaborate on the question.
In Python books, it is written that everything in Python is an object.

Let's take the traditional approach to learning/teaching programming languages. Languages like Pascal, C, C++ have the concept of a "variable name" (identifier). A value is bound/assigned to the variable name. The value is stored in memory. The variable name is a way (in the source code of the program) to refer to a memory cell to get the value stored there.

I don't know if this statement is correct (for compiled languages): For variables, for example, of integer type, at the moment of program execution on the computer, the processor already works with the addresses of memory cells where the variable values are stored. That is, there is no such thing as a variable name being stored somewhere at some address and being linked to the address of the cell where the

Avatar ·

"

At different times, a different object can correspond to the same object.

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

The model in Python is simple — there's no need to know what an address, memory cell, or pointer is. It's enough to know only about names and the objects they are bound to.

Here, labels and balls are sufficient to explain any behavior at the Python level — at first it's hard to believe that such a simple model covers all edge cases.

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

You only have one object (the integer one). Small integers are cached in CPython. Different names a, b refer to the same object: a is b

Log in to leave an answer

Blogs