On 17 January 2013 10:44, Ilkka Pelkonen <ica@iki.fi> wrote:
Hi all, I ran into an issue in expression evaluation with Python for Windows 2.7.3. Consider the following code:
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.
To me as a primarily C++ programmer it seems there could be two different changes here, either change the behavior of the 'and' expression, forcing it to return Boolean even if the latter part is not evaluated, and/or make the comparison "False == None" return True.
Hi Ikka.. My personal suggestion - rewrite your code to read: flag = bool(element and element.is_visible()) instead. That way you don't have to mention trying to change a 20 year old behavior in a language with billions of lines of code in the wild which should be kept compatible, at expense of thinking your expressions. Nor wait for the next major "4.0" release of Python for being able to write your code. js -><-