Newbie: is a variable defined?
Jeremy Yallop
jeremy at jdyallop.freeserve.co.uk
Sat Dec 21 15:59:57 EST 2002
* Chad Netzer
| The preferred way to test against None is with the "is" keyword,
| presumably because something can equal None without being None
That is a (fairly remote) possibility. A more practical reason (IMO)
is that the test for identity is faster than the test for equality.
This is likely to be true in any language (consider comparing pointers
versus comparing strings in C, or `eq?' versus `equal?' in Scheme).
Anyway, here's Python (please excuse the long lines):
>>> profile.run('[x for x in xrange(5000000) if x is None]')
2 function calls in 7.300 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 7.300 7.300 7.300 7.300 <string>:1(?)
1 0.000 0.000 7.300 7.300 profile:0([x for x in xrange(5000000) if x is None])
0 0.000 0.000 profile:0(profiler)
>>> profile.run('[x for x in xrange(5000000) if x == None]')
2 function calls in 9.210 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 9.210 9.210 9.210 9.210 <string>:1(?)
1 0.000 0.000 9.210 9.210 profile:0([x for x in xrange(5000000) if x == None])
0 0.000 0.000 profile:0(profiler)
Jeremy.
More information about the Python-list
mailing list