On 17 January 2013 11:10, Ilkka Pelkonen
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.
Ikka, Python is a dynamic typed language. As such, there is no strict type checking for most operations. The behavior of boolean operations for Python 2.x is well defiend and described here: http://docs.python.org/2/reference/expressions.html#boolean-operations If you are testing for "truthfullness" of a given object, using the "==" value for that, as in your "if flag == expected_result:" is definetelly a non-recomended pratice. Objects that have False or True value have always been well defined in Python, and that definition follows common sense closely, on what should be False. No one would expect "None" to be True. The behavior of yielding the first part of the expression in a failed "and" operation is not unique to Python, and AFAIK, has been inspired from C - and there are tons of code which rely directly on this behavior. (Even though I'd agree a lot of this code, emulating the ternary operator in a time before it was available in Python 2.5 is very poorly written)
Regards, Ilkka