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
Post a Comment