why is self not passed to id()?

Terry Reedy tjreedy at udel.edu
Thu Sep 4 19:54:56 EDT 2008



Ruediger wrote:

> I am aware that id is a built in function why shouldn't i use it?

I consider this a sensible thing to have tried, but I an not too 
surprised it does not work because I am aware that built-in functions do 
not have all the features of Python function.

I have asked about this case on Py-dev.
Subject: Can/should built-in functions get __get__?

> Replaceing lambda with id was intended as an performance hack. Profiling
> proofed that lambda itself takes more than twice as much cpu time than id
> alone. (profile shortened)
> 
>          3610503 function calls in 22.451 CPU seconds
> 
>    Ordered by: standard name
> 
>    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
> 
>    960096    4.593    0.000    6.702    0.000 test14.py:33(<lambda>)
>         1    0.003    0.003   22.451   22.451 {execfile}
>    960096    2.109    0.000    2.109    0.000 {id}
> 
> However using lambda seemed useless to me since id already took an argument
> and wrapping it in an python function simply has no real purpose.

Ironically, such simple wrappings are usually considered bad form.

There *is* a third alternative, which works in this case, and which 
should be closer in speed to id.  I will leave you to do a speed test.

 >>> class bang(list):
	__hash__ = object.__hash__

 >>> s=set()
 >>> s.add(bang())
 >>> s
{[]}

  __eq__ = object.__eq__ should also work instead of the Python 
implementation I gave in my response to another response.

Terry Jan Reedy




More information about the Python-list mailing list