>>> def foo(a):
print "called the function"
if(a==1):
return 1
else:
return None
>>> a=1
>>> if(foo(a) != None and foo(a) ==1):
print "asdf"
called the function
called the function
asdf
Hi. how can i avoid calling the function twice without using an extra variable.
You can chain the comparisons like this
if None != foo(a) == 1:
This works like
if (None != foo(a)) and (foo(a) == 1):
except that it only evaluates foo(a) once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With