tags:

views:

54

answers:

3

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 represents and what could i do with that hexadecimal number in python?

+2  A: 

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.

RichieHindle
Thanks for your response, x is y is a nice example.
systempuntoout
+5  A: 
>>> 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.

ChristopheD
So, it's not stricly related to address memory.
systempuntoout
This means that there's no language specification that there should be a 'hard' correspondence to address memory (there is in the canonical CPython implementation, but this could be different in Jython, Cython, ...)
ChristopheD
Note: Cython isn't really a different implementation, it's a language for making CPython C extensions. (The underlying message, that Jython and IronPython and PyPy could handle this differently is of course true.)
Mike Graham
Good point about Cython. It's a little to late to edit my original comment, but thanks for noticing.
ChristopheD
A: 

It is the memory offset of the Python object. Two objects are strictly same if they have same memory offset.

In Java, the similar thing is the hashCode.

SHiNKiROU
`memory offset` is (per the docs) CPython implementation specific
ChristopheD