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


Groups > comp.lang.python > #49613

Re: Stupid ways to spell simple code

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <mszamot@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; '"""': 0.07; 'subject:code': 0.07; 'raises': 0.09; 'def': 0.12; 'useful,': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; 'does,': 0.16; 'f(arg):': 0.16; 'subject:simple': 0.16; 'true:': 0.16; 'wraps': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'mon,': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'url:mailman': 0.30; '+0100,': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'supposed': 0.32; 'another': 0.32; 'quite': 0.32; 'url:python': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; "you're": 0.61; 'first': 0.61; 'content- disposition:inline': 0.62; 'different': 0.65; 'here': 0.66; 'jul': 0.74; 'trick,': 0.84; 'not:': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=VCYmpyMw8Obn7ppZDNMn/jnxK0oTcfVL1o5Dyd3gFpc=; b=kbFh6EhX8jEsRgvvQJWbjE6x9jV4myjvVG8ScsgPE7Mw4bOFxef77+wlTSHpGF81wm zZ2W93IwgORFOhVnPRrHS9pQvPHq7uoz/RZd+nZ42kVwAONkFnniyu3PK52qFF8n5FYw IYl/ZRey/jX27EaZIyRHBBaY2P8Napm8PW4z/92DpujI0ie/P2rzCznyz0rNTS5ugxSo S2MugWWCMssj2dqsA2qmEcQgELFyCVC4rWSGShvjU3LC2QjNKT6/17Q4+cLkyP9qu438 dXLGbOVOt9L1gnpVrGlp+uYWvuqOXjQga/qRS7u7X4+uo50Bss8TBr5outuNhPytZ2yL bKSQ==
X-Received by 10.194.174.137 with SMTP id bs9mr22203415wjc.59.1372746138352; Mon, 01 Jul 2013 23:22:18 -0700 (PDT)
Date Tue, 2 Jul 2013 07:22:10 +0100
From Marcin Szamotulski <mszamot@gmail.com>
To python-list@python.org
Subject Re: Stupid ways to spell simple code
References <mailman.4021.1372572404.3114.python-list@python.org> <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>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <51d1fe0d$0$29973$c3e8da3$5496439d@news.astraweb.com>
User-Agent Mutt/1.5.21 (2010-09-15)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4094.1372746145.3114.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372746145 news.xs4all.nl 15871 [2001:888:2000:d::a6]:39065
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49613

Show key headers only | 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