how do i get the value of a function i’m using to trigger an if condition in python?
something like…
def is_a_banana(a):
if a == "Banana":
return True
else:
return False
v = "Banana"
if ban = is_a_banana(v):
print ban
>> True
i know i could just split it into…
ban = is_a_banana(v)
if ban:
print ban
but i’m trying to rewrite the following in an efficient way so that i can get the returned values of the functions out. in php i could just bind the output to a variable, is their any way to do this with python?
if s.real_quick_ratio() >= cutoff and \
s.quick_ratio() >= cutoff and \
s.ratio() >= cutoff:
result.append((s.ratio(), x))
Assignment does not return a value in Python. It’s actually not an expression, but a statement rather, so it always has to be by itself. That will never change. It’s mainly a design decision by the Python developers; they found that that particular idiom in C made code less readable so it was left out (there’s also some speculation that it was left out to avoid mixups between
=and==, but I don’t buy that).You can write the second form if you’d like, or you can work out a different way to do what you need.