[Python-ideas] Fwd: Boolean behavior of None

Oleg Broytman phd at phdru.name
Thu Jan 17 13:51:05 CET 2013


On Thu, Jan 17, 2013 at 02:44:03PM +0200, Ilkka Pelkonen <ica at iki.fi> wrote:
> expected_result = (expected_string != 'TRUE') # Boolean
> element = find_element() # Can return None or an instance of Element
> flag = (element and element.is_visible())
> if flag == expected_result:
> ..# 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 expected_result is False, since boolean False
> is not None.

   No need to change the language. Just do

flag = bool(element and element.is_visible())

Oleg.
-- 
     Oleg Broytman            http://phdru.name/            phd at phdru.name
           Programmers don't die, they just GOSUB without RETURN.



More information about the Python-ideas mailing list