开发者

how do i get the value of a function i'm using in an if condition in python?

开发者 https://www.devze.com 2023-02-18 05:42 出处:网络
how do i get the value of a function i\'m using to trigger an if condition in python? something like...

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))


Update: other answers satisfactorily explained why you can't do what you want directly in Python - I added this just to show that a version that only calculates each ratio a minimum number of times (so still has good performance) is still readable and clear...

Personally, I think this is pretty clear:

if s.real_quick_ratio() >= cutoff and s.quick_ratio() >= cutoff:
    r = s.ratio()
    if r >= cutoff:
        result.append((r, 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.


You should do it the simple way: assign explicitly to a variable and look at the value of the variable.

You could do something like this:

def save(box,name,value):
    box[name] = value
    return value

box = {}
if save(box,'real_quick',s.real_quick_ratio()) >= cutoff \
   and save(box,'quick',s.quick_ratio()) >= cutoff \
   and save(box,'ratio',s.ratio()) >= cutoff:
    # do something with box['ratio']

but it's horrible. Why not just make s.ratio() cache the value it's computing?


You simply can not do this beause in Python this

ban = is_a_banana(v)

is a statement, not an expression, thus is doesn't return a value that you can check on.

Keep it how it is, because not only is it readable but it also works.


Maybe this can be handled by enabling caching inside function(or decorator) like:

class Factorial(object):
    def __init__(self,arg):
        self.arg = arg
    @property
    def value(self):
        if self.arg == 1 : return 1
        else: return self.arg * Factorial(self.arg - 1).value

class CachedFactorial(Factorial):
    @property
    def value(self):
        if not hasattr(self,'_value'): 
            self._value = super(CachedFactorial,self).value
        return self._value
0

精彩评论

暂无评论...
验证码 换一张
取 消