Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27748

Re: Guarding arithmetic

From rusi <rustompmody@gmail.com>
Newsgroups comp.lang.python
Subject Re: Guarding arithmetic
Date 2012-08-23 10:15 -0700
Organization http://groups.google.com
Message-ID <e68c7216-5d08-444f-9424-e7f8561f9754@qa3g2000pbc.googlegroups.com> (permalink)
References <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> <mailman.3707.1345716686.4697.python-list@python.org>

Show all headers | View raw


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?
>
> > 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?
>
> 1/0 is evaluated before safe() is called. Therefore safe() has no chance to
> catch the exception. You have to move the evaluation into the safe()
> function:
>
> >>> def safe(deferred, default=42, exception=Exception):
>
> ...     try:
> ...             return deferred()
> ...     except exception:
> ...             return default
> ...>>> print safe(lambda: 666)
> 666
> >>> print safe(lambda: 1/0)
>
> 42

Nice!

Functional programmers know that once you have a lazy language, you
can simulate all control constructs within that framework. eg in
Haskell one could define an 'if-function' as
iffunc (True,  a, b) = a
iffunc (False, a, b) = b
In most other languages such a definition would not work.

What Peter has demonstrated is that for an eager language like python,
a bare-lambda is a lazy-fying construct

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:05 -0700
  Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-23 19:16 +1000
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:22 -0700
    Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-23 19:29 +1000
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:22 -0700
  Re: Guarding arithmetic Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-23 11:23 +0200
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:47 -0700
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:47 -0700
  Re: Guarding arithmetic Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-23 11:28 +0200
  Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-23 19:30 +1000
  Re: Guarding arithmetic Peter Otten <__peter__@web.de> - 2012-08-23 12:11 +0200
    Re: Guarding arithmetic rusi <rustompmody@gmail.com> - 2012-08-23 10:15 -0700
  Re: Guarding arithmetic Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-23 13:01 +0200
  Re: Guarding arithmetic MRAB <python@mrabarnett.plus.com> - 2012-08-23 12:21 +0100
  Re: Guarding arithmetic Peter Otten <__peter__@web.de> - 2012-08-23 13:28 +0200
  Re: Guarding arithmetic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-23 15:11 +0100
  Re: Guarding arithmetic Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-23 14:49 -0400
  Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-24 07:48 +1000

csiph-web