[Tutor] Is "var = None" in Python equivalent to "Set var

Kent Johnson kent37 at tds.net
Mon Jun 30 12:46:19 CEST 2008


On Mon, Jun 30, 2008 at 12:53 AM, wesley chun <wescpy at gmail.com> wrote:
>> As I understand it there are no cases where obj==None and obj is None
>>  will give different results (which is not true for == vs is in
>>  general), so is there any practical difference?  Maybe you save a
>>  handful of cycles?
>
>
> as far as i know, that's it. but if this comparison happens a *lot* in
> your code, it starts to add up.

I tend to use "is None" because I think it is more readable and
explicit, but I would say using it for performance is premature
optimization.

kent $ python -m timeit -s "a=3" "a==None"
10000000 loops, best of 3: 0.117 usec per loop
kent $ python -m timeit -s "a=3" "a is None"
10000000 loops, best of 3: 0.0608 usec per loop

You would have to be doing quite a few comparisons for .06 usec to
make a difference.

Kent


More information about the Tutor mailing list