This is a valid syntax in JavaScript:
const myVar = true; myVar && myFunction() // function will be executed
The same behavior can be achieved like this in Python:
myVar = True if myVar: myFUnction() # function will be executed
Is there a Pythonic way how to get the same one-liner like in Javascript without explicitly using an if
statement?
Advertisement
Answer
and
is a short-circuit in python (as explained here), so
myVar and myFunction()
will give you what you want