Perl and Python, a practical side-by-side example.

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Mar 4 22:22:46 CET 2007


Bjoern Schliessmann a écrit :
> Bruno Desthuilliers wrote:
> 
>>Shawn Milo a écrit :
> 
> 
>>>    if recs.has_key(piid) is False:
>>
>>'is' is the identity operator - practically, in CPython, it
>>compares memory addresses. You *dont* want to use it here.
> 
> 
> It's recommended to use "is None"; why not "is False"? Are there
> multiple False instances or is False generated somehow?

Once upon a time, Python didn't have a "proper" boolean type. It only 
had rules for boolean evaluation of a given object. According to these 
rules - that of course still apply -, empty strings, lists, tuples or 
dicts, numeric zeros and None are false in a boolean context. IOW, an 
expression can eval to false without actually being the False object 
itself. So the result of using the identity operator to test against 
such an expression, while being clearly defined, may not be exactly what 
you'd think.

To make a long story short:

if not []:
   print "the empty list evals to false in a boolean context"

if [] is False:
   print "this python interpreter is broken"

HTH



More information about the Python-list mailing list