python - Abstracting if statement and return by a function -


i have function this:

def test():     x = "3" # in actual code, computed      if x none:         return none      y = "3"      if y none:         return none      z = "hello"      if z none:         return none 

is there way of making if statement go away , abstract function. i'm expecting this:

def test():     x = "3"     check_none(x)      y = "3"     check_none(y)      z = "hello"     check_none(z) 

ideally, check_none should alter control flow if parameter passed none. possible?

note: working on python 2.7.

you can code in thing this.

def test():     #compute x, y, z     if none in [x, y, z]:        return none     # proceed rest of code 

an better way use generator generate value x, y, z computation 1 value @ time.

def compute_values():     yield compute_x()     yield compute_y()     yield compute_z()  def test():     value in compute_values():         if value none:            return none 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -