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


Groups > comp.lang.python > #49613

Re: Stupid ways to spell simple code

Date 2013-07-02 07:22 +0100
From Marcin Szamotulski <mszamot@gmail.com>
Subject Re: Stupid ways to spell simple code
References (1 earlier) <b3dcp4Fhl04U2@mid.individual.net> <CAPTjJmpvPPkM8sDaDu_jwXW1kHvhfrbN4niWeE3b_mX5SeSkyA@mail.gmail.com> <CAN1F8qUkstvGHdxRN0at-z3_5iXi4XKY36Xmnf-fnsFuPY+mAg@mail.gmail.com> <mailman.4082.1372707405.3114.python-list@python.org> <51d1fe0d$0$29973$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.4094.1372746145.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 22:09 Mon 01 Jul     , Steven D'Aprano wrote:
> On Mon, 01 Jul 2013 20:36:29 +0100, Marcin Szamotulski wrote:
> 
> > Here is another example which I came across when playing with
> > generators, the first function is actually quite useful, the second
> > generator is the whole fun:
> > 
> > from functools import wraps
> > def init(func):
> >     """decorator which initialises the generator """
> >     @wraps(func)
> >     def inner(*args, **kwargs):
> >         g = func(*args, **kwargs)
> >         g.send(None)
> >         return g
> >     return inner
> > 
> > @init
> > def gen(func):
> >      x = (yield)
> >      while True:
> >          x = (yield func(x))
> > 
> > 
> > now if you have function f
> > def f(arg):
> >     return arg**2
> > 
> > then calling f(5) is the same as
> > 
> > g = gen(f)
> > g.send(5)
> 
> 
> 
> I think you must be missing an important part of the trick, because 
> calling f(5) returns 25. It's not:
> 
> @gen
> def f(arg):
>     return arg**2
> 
> 
> because that raises TypeError.
> -- 
> Steven
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Sure it does,  you're now supposed to use .send method instead of
calling it but this is just different syntax.  If you want to call it
use this :

def identity(func):
    @init
    def gen(func):
        x = (yield)
        while True:
            x = (yield func(x))
    return gen(func).send

Now you will get:

>>> @identity
>>> def f(a): a+1
...
>>> f(0)
1

Best regards,
Marcin

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


Thread

Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-06-30 16:06 +1000
  Re: Stupid ways to spell simple code Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-30 07:58 -0700
    Re: Stupid ways to spell simple code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-30 16:08 +0100
    Re: Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-07-01 01:20 +1000
    Re: Stupid ways to spell simple code Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 11:26 -0600
  Re: Stupid ways to spell simple code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-30 17:36 +0000
    Re: Stupid ways to spell simple code Roy Smith <roy@panix.com> - 2013-06-30 13:44 -0400
    Re: Stupid ways to spell simple code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-30 22:52 +0100
  Re: Stupid ways to spell simple code Neil Cerutti <neilc@norwich.edu> - 2013-07-01 12:59 +0000
    Re: Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-07-01 23:14 +1000
    Re: Stupid ways to spell simple code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-01 17:30 +0100
    Re: Stupid ways to spell simple code Marcin Szamotulski <mszamot@gmail.com> - 2013-07-01 20:36 +0100
      Re: Stupid ways to spell simple code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-01 22:09 +0000
        Re: Stupid ways to spell simple code 88888 Dihedral <dihedral88888@gmail.com> - 2013-07-01 21:16 -0700
        Re: Stupid ways to spell simple code Marcin Szamotulski <mszamot@gmail.com> - 2013-07-02 07:22 +0100
    Re: Stupid ways to spell simple code Chris Angelico <rosuav@gmail.com> - 2013-07-02 02:32 +1000
  Re: Stupid ways to spell simple code Russel Walker <russ.pobox@gmail.com> - 2013-07-02 00:33 -0700

csiph-web