views:

131

answers:

3

An ordinary object, I can use

o.__repr__()

to see something like

'<__main__.A object at 0x9d78fec>'

But, say, a Django User just returns

<User:bob>

How can I see the actual address of one of these, or compare whether two such model-objects are actually the same object or not?

+1  A: 

Use the id() function for that. Or equivalently just compare "obj1 is obj2".

Ants Aasma
+7  A: 

id() will return the identity of the object (generally implemented as the address), which is guaranteed unique for two objects which exist at the same point in time. However the obvious way to check whether two objects are identical is to use the operator explicitely designed for this: is

ie.

 if obj1 is obj2: 
     # Objects are identical.
Brian
+2  A: 

You can get the id of any object:

a = object()
print hex(id(a))

Although for CPython, this is the address of the object, this is not guaranteed I believe (and may be different on other implementations like iron python). Same id means same object, though.

David Cournapeau