Why is that invalid
print('true') if False else print('false')
but this one is not
def p(t):
print(t)
p('true') if False else p('false')
Why is that invalid
print('true') if False else print('false')
but this one is not
def p(t):
print(t)
p('true') if False else p('false')
In Python 2,
print
is a statement and therefore cannot be directly used in the ternary operator.In Python 3,
print
is a function and therefore can be used in the ternary operator.In both Python 2 and 3, you can wrap
print
(or any control statements etc) in a function, and then use that function in the ternary operator.