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: 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 To: python-list@python.org Subject: Re: Stupid ways to spell simple code References: <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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