Re: Guarding arithmetic

2012-08-23 Thread Chris Angelico
On Fri, Aug 24, 2012 at 4:49 AM, Dennis Lee Bieber wrote: > By the time you wrap the equation with a lambda all, named, terms, > AND supply the named terms after the lambda, you might as well just wrap > the equation in a plain try/except block. But closures spare you that hassle. >>> a=

Re: Guarding arithmetic

2012-08-23 Thread rusi
On Aug 23, 3:11 pm, Peter Otten <__pete...@web.de> wrote: > 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?

Re: Guarding arithmetic

2012-08-23 Thread Mark Lawrence
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 4

Re: Guarding arithmetic

2012-08-23 Thread Oscar Benjamin
On 23 August 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 sa

Re: Guarding arithmetic

2012-08-23 Thread Peter Otten
Laszlo Nagy wrote: > def safe(deferred, default=42, exception=Exception): >> ... try: >> ... return deferred() >> ... except exception: >> ... return default > > What a beautiful solution! I was wondering if the following would be > possible: > > > def test(t

Re: Guarding arithmetic

2012-08-23 Thread MRAB
On 23/08/2012 12:01, Laszlo Nagy wrote: def safe(deferred, default=42, exception=Exception): ... try: ... return deferred() ... except exception: ... return default What a beautiful solution! I was wondering if the following would be possible: def test(thing

Re: Guarding arithmetic

2012-08-23 Thread Laszlo Nagy
def safe(deferred, default=42, exception=Exception): ... try: ... return deferred() ... except exception: ... return default What a beautiful solution! I was wondering if the following would be possible: def test(thing, default, *exc_classes): try:

Re: Guarding arithmetic

2012-08-23 Thread Peter Otten
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 > >

Re: Guarding arithmetic

2012-08-23 Thread Mark Carter
On Thursday, 23 August 2012 10:23:20 UTC+1, Laszlo Nagy wrote: > On 2012-08-23 11:05, Mark Carter wrote: > You are very vague. "There is an error" - but what kind of error? Assume that it doesn't matter. > In some special cases, this can be a good idea to do. Those are the cases that I'm inte

Re: Guarding arithmetic

2012-08-23 Thread Chris Angelico
On Thu, Aug 23, 2012 at 7:28 PM, Laszlo Nagy wrote: >> That can work ONLY if the division of 1/0 doesn't raise an exception. >> This is why the concept of NaN exists; I'm not sure if there's a way >> to tell Python to return NaN instead of bombing, but it's most likely >> only possible with floati

Re: Guarding arithmetic

2012-08-23 Thread Chris Angelico
On Thu, Aug 23, 2012 at 7:22 PM, Mark Carter 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 e

Re: Guarding arithmetic

2012-08-23 Thread Laszlo Nagy
On 2012-08-23 11: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 4

Re: Guarding arithmetic

2012-08-23 Thread Laszlo Nagy
That can work ONLY if the division of 1/0 doesn't raise an exception. This is why the concept of NaN exists; I'm not sure if there's a way to tell Python to return NaN instead of bombing, but it's most likely only possible with floating point, not integer. For integers, Python will always raise

Re: Guarding arithmetic

2012-08-23 Thread Mark Carter
On Thursday, 23 August 2012 10:16:08 UTC+1, Chris Angelico wrote: > On Thu, Aug 23, 2012 at 7:05 PM, 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. > only possible with floating point, not

Re: Guarding arithmetic

2012-08-23 Thread Chris Angelico
On Thu, Aug 23, 2012 at 7:05 PM, 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 > pr

Guarding arithmetic

2012-08-23 Thread Mark Carter
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