Python id and '__main__' are different

Usually, if I assign a value to a variable and then check their identifiers, I expect them to be the same, because python essentially just gives my object a "name". This can be seen in the code below:

>>> a = 3
>>> id(a)
19845928
>>> id(3)
19845928

The problem is that I am doing the same thing with the name

>>> __name__
'__main__'
>>> id(__name__)
19652416
>>> id('__main__')
19652448

How can identifiers be different, should they not be the same? Because it __name__should also be just a link.

+3
source share
4 answers

id () gives essentially a memory pointer to data. Although the strings are immutable, they do not guarantee internment. This means that some lines with equal values ​​have different pointers.

( ) , 3 .


@KartikAnand: , " ", , x is y. , . . , "__main__", . python , .


Kartik , ", , " ". ( ).

>>> __name__
'__main__'
>>> x = __name__
>>> id(__name__)
3078339808L
>>> id(x)
3078339808L
>>> __name__ is x
True
+5

, , , . .

+4

Python "", . .

, , .

+3

, , ( ) Python. , , , , , id, - . , , id vs .

0
source

All Articles