Guarding arithmetic
Chris Angelico
rosuav at gmail.com
Thu Aug 23 05:29:20 EDT 2012
On Thu, Aug 23, 2012 at 7:22 PM, Mark Carter <alt.mcarter at gmail.com> wrote:
> OK, so it looks like a solution doesn't exist to the problem as specified. I guess it's something that only a language with macros could accommodate.
You're asking for a function to prevent the evaluation of its
arguments from throwing an error. That's fundamentally not possible
with a function call. Exception handling is a much more normal way of
handling it; though if you prefer, you could wrap it up in a function
like this:
def safe_div(num,denom):
try:
return num/denom
except ZeroDivisionError:
return 42
print safe_div(1,0) # still a poor name though
ChrisA
More information about the Python-list
mailing list