systempuntoout
systempuntoout

Reputation: 74124

What does the hexadecimal integer shown on print/repr in represent?

In an interactive session like the following one:

>>> f=open('test.txt','w')
>>> f
<open file 'test.txt', mode 'w' at 0x6e610>

What does 0x6e610 represent and what could I do with that hexadecimal number in Python?

Upvotes: 1

Views: 155

Answers (2)

ChristopheD
ChristopheD

Reputation: 116267

>>> f=open('test.txt')
>>> f
<open file 'test.txt', mode 'r' at 0x10047c938>
>>> hex(id(f))
'0x10047c938'

Have a look at id in the official documentation:

Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Upvotes: 6

RichieHindle
RichieHindle

Reputation: 281735

It's the ID of the object, which (in standard Python) is its address in memory.

You can also get it via the id(obj) function.

You can use IDs to tell whether two references refer to the same object - when you say if x is y in Python, you're comparing IDs.

Upvotes: 3

Related Questions