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

Fortepianissimo fortepianissimo at yahoo.com.tw
Tue May 20 00:59:49 EDT 2003


Bah I should've stared at my code a bit longer - it turns out I
changed the value part of a dictionary to a dictionary - and of course
an empty dictionary is false.

But thanks anyway.

Simon Brunning <SBrunning at trisystems.co.uk> wrote in message news:<mailman.1053359380.19104.python-list at python.org>...
> > 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