weird: "if x" and "if x is not None"

Simon Brunning SBrunning at trisystems.co.uk
Mon May 19 11:47:08 EDT 2003


> From:	fortepianissimo at yahoo.com.tw [SMTP:fortepianissimo at yahoo.com.tw]
> I don't know what has changed, but the following code used to work
> fine now it doesn't:
> 
> result=d.get("key")
> if result:
>   # do something...
> 
> 
> the problem seems to be at "if result:" - before when result is not
> None, the if block will be executed. Now I have to literally say "if
> result is not None:" instead of that line.
> 
> My question is, what does the 'standard' say about this? (any non-None
> will make an 'if' test succeed?)
 
The if block will be executed for any *true* value. None is a false value,
as are zero values and empty strings, sequences and mappings. See
<http://www.python.org/doc/current/ref/Booleans.html> for the official word.

I would be *staggered* if this had changed...

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.





More information about the Python-list mailing list