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=
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?
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
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
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
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
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:
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
>
>
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
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
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
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
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
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
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
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
16 matches
Mail list logo