This is a valid syntax in JavaScript:
JavaScript
x
3
1
const myVar = true;
2
myVar && myFunction() // function will be executed
3
The same behavior can be achieved like this in Python:
JavaScript
1
4
1
myVar = True
2
if myVar:
3
myFUnction() # function will be executed
4
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
JavaScript
1
2
1
myVar and myFunction()
2
will give you what you want