Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.04; 'subject:Python': 0.05; 'python': 0.08; 'def': 0.13; 'cc:addr :python-list': 0.15; 'antti': 0.16; 'closures': 0.16; 'curiosity,': 0.16; 'wrote:': 0.16; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'feb': 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.212.46': 0.23; 'received:mail- vw0-f46.google.com': 0.23; 'cc:2**0': 0.25; 'pm,': 0.26; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'fri,': 0.30; 'chris': 0.30; 'usually': 0.30; 'skip:- 30': 0.33; 'done': 0.33; 'read,': 0.33; 'follows:': 0.34; 'received:209.85.212': 0.34; 'received:google.com': 0.37; 'received:209.85': 0.38; 'several': 0.38; 'subject:with': 0.38; 'received:209': 0.39; 'subject:: ': 0.39; 'kept': 0.68; 'calls,': 0.84; 'sender:addr:chris': 0.84; '\xc2\xa0it': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=poIRTh5JZUbLAo3ODIGW5cFREct2G443dO7vXhgcYww=; b=AZW17zAGMi/E4C6ssBxcFPwxHDLL6UhSGW8llFf40lR1qemCSm1oyqtkulk512vEU9 YFrxrJi4dEsKz+y1Hho/qnZ4LMV4EF/MYM3c/HvEvpDWBN0e+HCEcJzx5z2uJeprC2r6 zAG78UbYR+IdvTQBeadPLK2p9auHw7D7iwQCU= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Fri, 3 Feb 2012 18:47:20 -0800 X-Google-Sender-Auth: 0TAlDte6wCiaDQD4T9tcXO1JgsU Subject: Re: Common LISP-style closures with Python From: Chris Rebert To: Antti J Ylikoski Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328323649 news.xs4all.nl 6951 [2001:888:2000:d::a6]:55064 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19852 On Fri, Feb 3, 2012 at 4:27 PM, Antti J Ylikoski wr= ote: > > In Python textbooks that I have read, it is usually not mentioned that > we can very easily program Common LISP-style closures with Python. =C2=A0= It > is done as follows: > > ------------------------------------- > > # Make a Common LISP-like closure with Python. > # > # Antti J Ylikoski 02-03-2012. > > def f1(): > =C2=A0 =C2=A0n =3D 0 > =C2=A0 =C2=A0def f2(): > =C2=A0 =C2=A0 =C2=A0 =C2=A0nonlocal n > =C2=A0 =C2=A0 =C2=A0 =C2=A0n +=3D 1 > =C2=A0 =C2=A0 =C2=A0 =C2=A0return n > =C2=A0 =C2=A0return 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. Out of curiosity, what would be non-Common-Lisp-style closures? Cheers, Chris