Is the same number changing its ID every time I try to run the program?

In Python, every integer has a ten-digit ID starting at 438. I was trying to find a number that matches its ID. I wrote a simple code to find the number:

for i in range(4380000000,4390000000):
    if i==id(i):
        print(i)
    else:
        pass

      

When I first ran this I didn't have that many. Then I ran it a second time and still don't have a number.

When I ran it a third time I got the number: 4384404848

Then I checked if id(4384404848)==4384404848

, and I received False

.

Why did Python return a number not equal to its id? Or did the same number have different identifiers when the program started and when it stopped?

(EDIT: The assumption "every integer seems to have a 10-digit identifier starting at 438" is incorrect.)

+3


source to share


3 answers


https://docs.python.org/2/library/functions.html#id

guaranteed to be unique and permanent to this property throughout its life.

Consider id as a unique identifier or "hash" calculated for this object. It may (and most likely will) be different each time you run your program.

Edit: just add if you are using CPython implementation (which is the most popular) then this is the address of the object in memory. This should clarify why it was not the same in different runs of the same program.



As a separate note, you should never rely on the id () value for any object other than its uniqueness for a given given one.

each integer is like a 10-digit identifier starting at 438

- incorrect assumption. On my machine:

>>> x = 5
>>> id(x)
38888712L

      

+4


source


NOT

It doesn't always start with 438

You should think of it as a unique case number for a college student or employee number (but for Python objects)

See what the docs say

identifier (object)

Return the "id" of the object. It is an integer that is guaranteed to be unique and constant for this object throughout its life. Two objects with non-overlapping lifetime can have the same id () value.

CPython implementation details: this is the address of the object in memory.

To make everything clear. I assume you know that no matter how many variables you create, if they contain the same value , then in Python they are all the same. ( Aliases )

Look at the translator.

>>> a=10
>>> id(10)
26775680
>>> b=20
>>> id(20)
26775440

      

The only right. Now let's see

>>> a=10
>>> b=10
>>> id(a)
26775680
>>> id(b)
26775680

      

Also see

>>> a=10
>>> id(a)
26775680
>>> a=20
>>> b=a
>>> id(a)
26775440
>>> id(b)
26775440

      

Therefore, each value (objects) is assigned a unique value. And that value is nothing but your id () .

Since the OP asked!



Python implementations.

Value:

A Python "implementation" is to be understood as a program or environment that provides support for the execution of programs written in the Python language, as represented by the referenced CPython implementation.

So this means that Cpython is a language engine that runs Python code (language). Why is it called Cpython? To distinguish Python (language) from Cpython (implementation).

So basically Cpython is the one that is the most common Python implementation (CPython: written in C, often referred to simply as "Python") The one you download from python.org is

You need to differentiate between language and implementation. Python is a language.

According to Wikipedia,

"A programming language is a designation for writing programs that are specifications of a computation or algorithm."

This means that these are just rules and syntax for writing code. Separately, we have

an implementation of a programming language which in most cases is the actual interpreter or compiler.

So CPython is a C implementation

There's Jython - implementation in Java

IronPython - implementation in C #

And a few more. Take a look at them here Implementations . Download and chat with them to find out more.

+2


source


According to the official doc:

Return the "id" of the object. This is an integer (or long integer) that is guaranteed to be unique and constant for this object throughout its life. Two objects with non-overlapping lifetime can have the same id () value.

Thus, in essence, the identifier of any object in Python is a hash value that is unique to it with a lifetime.

Why did Python return a number not equal to its id? Or did the same number have different identifiers when the program started and when it stopped?

You found the hash value "i" to be 4384404848, but you are comparing it to the hash value of the literal "4384404848". By definition, they should be different.

-1


source







All Articles