why do some short form of conditionals work in python and some don't? -



why do some short form of conditionals work in python and some don't? -

take example:

= "foo" if true else "bar"

this executes fine , without problems. take:

print("foo") if true else print("bar")

this throws error.

i assume first 1 works ternary operator. there way write sec statement without resorting total length:

if true: print("foo") else: print("bar")

something akin perl's

print("foo") if true

all paths of conditional expression, must evaluatable.

in python 2.7, print statement, not function. so, cannot evaluated expression.

since print statement violates point 2, cannot used. can do

print "foo" if true else "bar"

in python 3.x, print function, can write mentioned in question

print("foo") if true else print("bar")

since print function in python 3.x, result of function phone call result of evaluation of function phone call expression. can check this

print(print("foo") if true else print("bar")) # foo # none

print function doesn't explicitly homecoming anything. so, default, returns none. result of evaluating print("foo") none.

python python-2.7 conditional-statements

Comments

Popular posts from this blog

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -