Empty blocks – the pass statement
With that said, we still have the problem of what to do with our empty except
block. The solution arrives in the form of the pass
keyword, which is a special statement that does precisely nothing! It's a no-op, and it's only purpose is to allow us to construct syntactically permissible blocks that are semantically empty:
def convert(s): """Convert a string to an integer.""" x = -1 try: x = int(s) except (ValueError, TypeError): pass return x
In this case though, it would be better to simplify further by using multiple return
statements, doing away with the x
variable completely:
def convert(s): """Convert a string to an integer.""" try: return int(s) except (ValueError, TypeError): return -1