[Python-ideas] Fwd: Boolean behavior of None (original) (raw)
Ilkka Pelkonen ilkka.pelkonen at iki.fi
Thu Jan 17 14:10:45 CET 2013
- Previous message: [Python-ideas] Fwd: Boolean behavior of None
- Next message: [Python-ideas] Fwd: Boolean behavior of None
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Oleg, others, It's not that it can't be done, just that it does something you don't expect. I've been professionally working with C++ for nine years in large-scale Windows systems, and I do expect a boolean expression return a boolean value.
Or, can you show me an example how the developer would benefit of the current behavior? Any operator traditionally considered as boolean will do.
Regards, Ilkka
On Thu, Jan 17, 2013 at 2:51 PM, Oleg Broytman <phd at phdru.name> wrote:
On Thu, Jan 17, 2013 at 02:44:03PM +0200, Ilkka Pelkonen <ica at iki.fi> wrote: > expectedresult = (expectedstring != 'TRUE') # Boolean > element = findelement() # Can return None or an instance of Element > flag = (element and element.isvisible()) > if flag == expectedresult: > ..# Ok > ..return > # Otherwise perform some failure related stuff. > > This code does not work. What happens on the 'flag' assignment row, is that > if 'element' is None, the expression returns None, not False. This makes > the if comparison to fail if expectedresult is False, since boolean False > is not None.
No need to change the language. Just do flag = bool(element and element.isvisible()) Oleg. -- Oleg Broytman http://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN.
Python-ideas mailing list Python-ideas at python.org http://mail.python.org/mailman/listinfo/python-ideas -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130117/ff83ad34/attachment.html>
- Previous message: [Python-ideas] Fwd: Boolean behavior of None
- Next message: [Python-ideas] Fwd: Boolean behavior of None
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]