аватар on5fydu8@gmail.com · 01.01.1970 03:00

What is an object in Python. Why is ID (A) == ID (b) == ID (1)?

"

Detail the question.
In Python books, they write that everyone in Python has an object.

we take a traditional approach to leaing/teaching programming languages. Languages ​​Pascal, C, C ++, they have the concept of" variable name "(identifier). with the name of the variable. The value is binded/assigned to a variable name - this is a way (in the original text of the program) to contact a memory cell for obtaining the value that is stored there. That is, it is that somewhere in a variable is kept at a certain address and it is connected with the address of the cell.

now we go to Python. Object.

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

but then how to understand the action of the function of the function of the standard? Moreover, "" retus the whole 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 )) # trye    print  ( "Who?" ")     

A, B, 1 different objects? be

  a =  1000000  b =  1000000    print  ( id  (a) ==  id  (b)) # trye   )   

at the expense of" Know the names, etc. " Question.

"

аватар on5fydu8@gmail.com · 01.01.1970 03:00

"

at different times with the same object, a different object can be correlated.

You wanted to say: at different times, the same name may refer to different Objects

in Python is simple & mdash; it is absolutely not necessary to know what the address is, the pointer is enough to know about the names and objects to which they are attached. Extreme cases are covered by.

after A, B, 1 different objects?

You have only one object (unit). a is b (). .

a = 1000000; (Here it is equivalent to id (a) == ID (b) , but id can be redefined in the general case).

The result can be true (depended as a pytho code was compiled in a chosen realization), but must not be required True :

   & gt; & gt;  a =  & gt; & gt;  b =  1000000  & gt; & gt; class = ""> print  (a  is  b)   false      

in this case, each expression is separately compiled, a and B Completely compare:

   & gt; & gt; class = ""> def   f  ():   ...  a =  1000000    ...  b =  1000000    ...   print  (a  is  b)   & gt; & gt;  f ()   true      

the code as a whole is compiled, so a is b can be trial The object or somewhat dependent on the implementation of the python (Cpython, Pypy, Jython, ETC) and even a specific version of the implementation. Numbers.

Python is not the name of the variables. the question.

The fact that Python’s code in different ideas can be manipulated as a simple object (convey as a parameter, retuing from a function, calling methods, etc.) does not cancel the presence of names. _ast.name . Code, it may be useful to know about compile (), eval (), exec () built -in functions, DIS,, Inspect of the modules.

Latest

Similar