Guarding arithmetic

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Aug 23 10:11:43 EDT 2012


On 23/08/2012 10:05, Mark Carter wrote:
> Suppose I want to define a function "safe", which returns the argument passed if there is no error, and 42 if there is one. So the setup is something like:
>
> def safe(x):
>     # WHAT WOULD DEFINE HERE?
>
> print safe(666) # prints 666
> print safe(1/0) # prints 42
>
> I don't see how such a function could be defined. Is it possible?
>

Well you've already got lots of answers but I'm not certain about what 
you're trying to achieve.  If you could explicitly state your 
requirements I'm sure that the numerous MVPs (Most Valuable Pythonistas) 
here would come up with The Best Solution ™ to your problem.

-- 
Cheers.

Mark Lawrence.




More information about the Python-list mailing list