[Python-ideas] Fwd: Boolean behavior of None (original) (raw)
Ilkka Pelkonen ica at iki.fi
Thu Jan 17 13:44:03 CET 2013
- Previous message: [Python-ideas] The async API of the future
- Next message: [Python-ideas] Fwd: Boolean behavior of None
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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. Although potentially complex, I'd myself go for the first approach. It seems to me more logical that False != None than an 'and' expression returning non-boolean. Also the latter change might require people change their code, while the former should not require any modifications.
This behavior probably results in lots of errors when people like me, used to more traditional languages, take on Python in a serious manner. I like the concept 'pythonic', and am trying to apply it to practice like above.
Hoping to hear your thoughts, Regards,
Ilkka Pelkonen -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130117/732873ed/attachment.html>
- Previous message: [Python-ideas] The async API of the future
- Next message: [Python-ideas] Fwd: Boolean behavior of None
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]