Difference between 'is' and '=='

Clemens Hepper ethrandil at gmx.net
Mon Mar 27 08:09:52 EST 2006


Roy Smith wrote:
> In article <e08mr1$7lu$1 at news.lysator.liu.se>,
>  Joel Hedlund <joel.hedlund at gmail.com> wrote:
> 
>> Which means that "is" comparisons in general will be faster than == 
>> comparisons.
> 
> I thought that == automatically compared identify before trying to compare 
> the values.  Or am I thinking of some special case, like strings?

Even for strings there is a performance difference:

>>> timeit.Timer("'a'=='a'").timeit()
0.26859784126281738
>>> timeit.Timer("'a' is 'a'").timeit()
0.21730494499206543

mfg
- eth



More information about the Python-list mailing list