First page Back Continue Last page Overview Graphics

Another Example

It's easier to ask for forgiveness than permission!

#check whether int conversion will raise an error

if not isinstance(s, str) or not s.isdigit:

return None

elif len(s) > 10: # nonsense in Python !

return None

else:

return int(s)

Better:

try:

return int(s)

except (TypeError, ValueError, OverflowError): #int conversion failed

return None