``is`` vs ``==`` (was Re: Trivial performance questions)

Aahz aahz at pythoncraft.com
Sun Oct 19 11:11:47 EDT 2003


In article <Klckb.807073$YN5.806740 at sccrnsc01>,
Bryan  <belred1 at yahoo.com> wrote:
>
>can you explain in more detail why "if x is None:" is better/faster
>than "if x == None:"? i guess i don't fully understand "is". my fingers
>always seem to want to type "if not x:", but that is probably worse
>still since it includes (), [], {}, '', False, None .

When you want to check whether a target specifically points to the None
object, you can't use == because it calls a Python method that might
return None.  That's in addition to the fact that ``is`` is faster.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan




More information about the Python-list mailing list