How to assign a variable in an IF condition, and then return it?

前端 未结 8 1288
野趣味
野趣味 2020-11-27 06:05
def isBig(x):
   if x > 4: 
       return \'apple\'
   else: 
       return \'orange\'

This works:

if isBig(y): return isBig(y)
         


        
相关标签:
8条回答
  • 2020-11-27 07:01

    You could use a generator:

    def ensure(x):
        if x: yield x
    
    for fruit in ensure(isBig(y)):
        return fruit
    
    0 讨论(0)
  • 2020-11-27 07:02

    If you want to code in PHP (or C), code in it. Don't try to force its methods onto another language.

    One of the basic tenets behind Python (in my opinion) is its readability. You should be using:

    fruit = isBig(y)
    if fruit: return fruit
    

    I should also mention that your use of isXXX() is very strange; it's usually used to return boolean values. Especially in this case where you're using it in an IF statement.

    0 讨论(0)
提交回复
热议问题