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


Groups > comp.lang.python > #49606

Re: Stupid ways to spell simple code

Newsgroups comp.lang.python
Date 2013-07-01 21:16 -0700
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>
Message-ID <875111a8-4784-4590-aecd-3c505e26126b@googlegroups.com> (permalink)
Subject Re: Stupid ways to spell simple code
From 88888 Dihedral <dihedral88888@gmail.com>

Show all headers | View raw


Steven D'Aprano於 2013年7月2日星期二UTC+8上午6時09分18秒寫道:
> 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

Lets be serious about generators and iterators.

A generator can be used only once in a program
is different from a a generator method of a class that 
can produce several instances with generators of the same kind
but operated in each instance of the class.

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