python - what is better approach to do? -



python - what is better approach to do? -

i have piece of cake returning none in case exception caught.

def getobject(ver): av = none try: av = gettestobject(ver) except exceptions.objectnotfound, err: _log.info("%r not found", obj.fromtestobject(ver)) homecoming av

or improve ?

def getobject(ver): try: av = gettestobject(ver) except exceptions.objectnotfound, err: _log.info("%r not found", obj.fromtestobject(ver)) else: homecoming av

a shorter (more pythonic, imho) equivalent first code snippet follows:

def getobject(ver): try: homecoming gettestobject(ver) except exceptions.objectnotfound, err: _log.info("%r not found", obj.fromtestobject(ver))

this works because python functions implicitly homecoming none if command reaches end of function without encountering return (as case if exception caught).

python design-patterns

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -