Different results between old and new ternary conditional operators in Python -


just small question ternary conditional operator has confused me long time.

code example (python 2.7):

>>> x, y = none, 3 >>> x, y (none, 3) >>> (x == none , x or y) 3 >>> (x != none , x or y) 3 >>> (x if x == none else y) 

the third , forth lines old-style conditional operator. both give same result. , obviously, former takes "wrong" result. maybe it's not wrong according python. it's easy make mistake in program , without explicit error.

the fifth line new-style version 2.5 according book "core python programming" (wesley j. chun) , return right.

does know this?

your third , fourth lines aren't ternary operator. they're attempt hack ternary operator using other operators, but, saw, don't work, because logical operators you're using depend on notion of "boolean falseness", not of condition, of results want return based on condition. real ternary operator added precisely don't have use sort of fakery anymore.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -