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


Groups > comp.lang.python > #19865

Re: Common LISP-style closures with Python

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <johnmohagan@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.023
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'python.': 0.04; 'subject:Python': 0.05; 'attribute': 0.07; 'storing': 0.09; 'def': 0.13; 'antti': 0.16; 'attribute:': 0.16; 'f()': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'sender:addr:gmail.com': 0.24; 'sat,': 0.25; 'function': 0.27; '+0200': 0.28; "i'm": 0.28; 'received:209.85.210.46': 0.30; 'received:mail-pz0-f46.google.com': 0.30; 'done': 0.34; 'to:addr :python-list': 0.35; 'but': 0.37; 'charset:us-ascii': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'subject:with': 0.37; 'received:209.85': 0.38; 'several': 0.38; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'john': 0.61; 'received:203': 0.61; 'header:Message-Id:1': 0.62; 'enclosed': 0.68; 'kept': 0.68; 'calls,': 0.84; 'naughty': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:date:from:to:subject:message-id:in-reply-to:references :x-mailer:mime-version:content-type:content-transfer-encoding; bh=4IcJ7xGLpjWqVXjEBVPC5cMSZvOKnKeFIu1XGNDwk6w=; b=kmbOpSkIl1JpZ1vhQv7etFPhU2DUkUyEMIDGI/1Eq1aTrShpGQq8mJKauiJYO7zhSk J6Og2RRJzr36qVwolwuINHLyTF/auQWn5rry395NFlvdOxqlL54q5q/Ud4y06XcvWG7J hvLWGFnWZ/hLIGNAZL6q9M4fb/JvILWWXg/CA=
Sender "John O'Hagan" <johnmohagan@gmail.com>
Date Sun, 5 Feb 2012 12:31:12 +1100
From John O'Hagan <research@johnohagan.com>
To python-list@python.org
Subject Re: Common LISP-style closures with Python
In-Reply-To <dY_Wq.11747$I33.10275@uutiset.elisa.fi>
References <dY_Wq.11747$I33.10275@uutiset.elisa.fi>
X-Mailer Sylpheed 3.2.0beta5 (GTK+ 2.24.8; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.5439.1328405488.27778.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328405488 news.xs4all.nl 6872 [2001:888:2000:d::a6]:45381
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19865

Show key headers only | View raw


On Sat, 04 Feb 2012 02:27:56 +0200
Antti J Ylikoski <antti.ylikoski@tkk.fi> wrote:

[...]


> 
> # Make a Common LISP-like closure with Python.
> #
> # Antti J Ylikoski 02-03-2012.
> 
> def f1():
>      n = 0
>      def f2():
>          nonlocal n
>          n += 1
>          return n
>      return f2
>
 
[...]

> 
> i. e. we can have several functions with private local states which
> are kept between function calls, in other words we can have Common
> LISP-like closures.
> 

I'm not sure how naughty this is, but the same thing can be done without using
nonlocal by storing the local state as an attribute of the enclosed function
object:

>>> def f():
...     def g():
...             g.count += 1
...             return g.count
...     g.count = 0
...     return g
... 
>>> h = f()
>>> j = f()
>>> h()
1
>>> h()
2
>>> h()
3
>>> j()
1
>>> j()
2
>>> j()
3

This way, you can also write to the attribute:

>>> j.count = 0
>>> j()
1


John

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


Thread

Common LISP-style closures with Python Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-04 02:27 +0200
  Re: Common LISP-style closures with Python Chris Rebert <clp2@rebertia.com> - 2012-02-03 18:47 -0800
    Re: Common LISP-style closures with Python Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-04 12:14 +0200
      Re: Common LISP-style closures with Python Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-04 12:23 +0200
      Re: Common LISP-style closures with Python Arnaud Delobelle <arnodel@gmail.com> - 2012-02-04 10:58 +0000
        Re: Common LISP-style closures with Python Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-04 15:09 +0200
          Re: Common LISP-style closures with Python Tomasz Rola <rtomek@ceti.pl> - 2012-02-04 18:42 +0100
      Re: Common LISP-style closures with Python Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-02-04 17:52 -0500
  Re: Common LISP-style closures with Python John O'Hagan <research@johnohagan.com> - 2012-02-05 12:31 +1100
    Re: Common LISP-style closures with Python Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-05 06:19 +0200
      Re: Common LISP-style closures with Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-05 13:58 -0700
        Re: Common LISP-style closures with Python Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-06 07:55 +0200
  Re: Common LISP-style closures with Python Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-05 15:29 -0800
  Re: Common LISP-style closures with Python John Nagle <nagle@animats.com> - 2012-02-09 22:26 -0800
  Re: Common LISP-style closures with Python 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-09 23:55 -0800

csiph-web