jeudi 22 octobre 2015

How can a function be run by a single-line if expression?

The value of a variable can be set in a single-line if expression in a way like the following:

x = 1 if a == 1 else 2

Can a function be run instead of a variable using this form of if expression? Conceptually, I mean something like the following:

print("1") if a == 1 else print("2")

I am aware that a single-line if statement can be used to run a function in a way like the following:

if a == 1: print("1")

Aucun commentaire:

Enregistrer un commentaire