Neeraj
Neeraj

Reputation: 201

The use of id() in Python

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?

Upvotes: 8

Views: 2137

Answers (5)

John Sallay
John Sallay

Reputation: 251

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.

Upvotes: 2

Duncan
Duncan

Reputation: 95642

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).

Upvotes: 1

Dan D.
Dan D.

Reputation: 74645

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
               key_id_row[key].add(id(row))

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

       seq = [ id_row_row[id_row] for id_row in intersect( key_id_row.values() ) ]
       return seq

Upvotes: 0

orlp
orlp

Reputation: 117661

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).

Upvotes: 6

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

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.

Upvotes: 1

Related Questions