Take the 2-minute tour ×

Of what use is id() in real-world programming? I have always thought this function is there just for academic purposes. Where would I actually use it in programming? I have been programming applications in Python for some time now, but I have never encountered any "need" for using id(). Could someone throw some light on its real world usage?

share|improve this question
@ThiefMaster: thank you for editing. –  Neeraj Apr 28 '11 at 4:10

5 Answers 5

It can be used for creating a dictionary of metadata about objects:

For example:

someobj = int(1)
somemetadata = "The type is an int"
data = {id(someobj):somemetadata}

Now if I occur this object somewhere else I can find if metadata about this object exists, in O(1) time (instead of looping with is).

share|improve this answer
Except as soon as the object goes away, the id is gone and can be replaced by a completely different object. –  Ignacio Vazquez-Abrams Apr 27 '11 at 9:10
@Ignacio Vazquez-Abrams: Well of course, but that's a completely different story (for this to be useful at all it must be wrapped in classes taking care of that stuff of course). –  orlp Apr 27 '11 at 9:14
The simplest way is to either store an (obj, metadata) 2-tuple, or else have a parallel id-to-object mapping in addition to the metadata mapping. –  ncoghlan Apr 27 '11 at 13:20

Anywhere where one might conceivably need id() one can use either is or a weakref instead. So, no need for it in real-world code.

share|improve this answer
exactly. is seems sufficient. –  Neeraj Apr 27 '11 at 9:05
Except that not everything permits weak references (e.g. try x = weakref.ref(1)). id is the only way to create fully general metadata stores that support arbitrary objects, including objects that cannot be hashed or weak referenced, while still allowing O(1) data lookup. –  ncoghlan Apr 27 '11 at 13:18

The only time I've found id() useful outside of debugging or answering questions on comp.lang.python is with a WeakValueDictionary, that is a dictionary which holds a weak reference to the values and drops any key when the last reference to that value disappears.

Sometimes you want to be able to access a group (or all) of the live instances of a class without extending the lifetime of those instances and in that case a weak mapping with id(instance) as key and instance as value can be useful.

However, I don't think I've had to do this very often, and if I had to do it again today then I'd probably just use a WeakSet (but I'm pretty sure that didn't exist last time I wanted this).

share|improve this answer
The weakref module exists since Python 2.1: docs.python.org/library/weakref.html –  Lie Ryan May 7 '11 at 3:03
Yes, but WeakSet only appeared in Python 3.x/2.7 –  Duncan May 7 '11 at 11:16

I use id() frequently when writing temporary files to disk. It's a very lightweight way of getting a pseudo-random number.

Let's say that during data processing I come up with some intermediate results that I want to save off for later use. I simply create a file name using the pertinent object's id.

fileName = "temp_results_" + str(id(self)).

Although there are many other ways of creating unique file names, this is my favorite. In CPython, the id is the memory address of the object. Thus, if multiple objects are instantiated, I'm guaranteed to never have a naming collision. That's all for the cost of 1 address lookup. The other methods that I'm aware of for getting a unique string are much more intense.

A concrete example would be a word-processing application where each open document is an object. I could periodically save progress to disk with multiple files open using this naming convention.

share|improve this answer

in one program i used it to compute the intersection of lists of non-hashables, like:

   def intersection(*lists):

       id_row_row = {} # id(row):row
       key_id_row = {} # key:set(id(row))

       for key, rows in enumerate(lists):
           key_id_row[key] = set()
           for row in rows:
               id_row_row[id(row)] = row

       from operator import and_
       def intersect(sets):
           if len(sets) > 0:
               return reduce(and_, sets)
               return set()

       seq = [ id_row_row[id_row] for id_row in intersect( key_id_row.values() ) ]
       return seq
share|improve this answer
Not entirely, this computes the intersection of objects, not values (two different string objects containing "string" will fail to intersect). –  orlp Apr 27 '11 at 9:16

Your Answer


