Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'cpython': 0.07; 'versions.': 0.07; 'python': 0.08; '[0,': 0.09; 'attribute': 0.09; 'decorator': 0.09; 'globals': 0.09; 'initialized': 0.09; 'referenced': 0.09; 'variables,': 0.09; '>>>': 0.12; 'def': 0.12; 'skip:[ 20': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'functools': 0.16; 'global,': 0.16; 'rachel': 0.16; '\xa0def': 0.16; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'fri,': 0.23; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'thus': 0.23; 'creating': 0.24; "doesn't": 0.25; 'pointed': 0.25; 'function': 0.25; 'changed': 0.25; 'received:209.85.161': 0.26; 'object': 0.26; 'example': 0.27; 'message-id:@mail.gmail.com': 0.28; 'changing': 0.28; 'import': 0.29; 'skip:( 20': 0.30; 'seem': 0.32; 'cases': 0.32; 'to:addr:python-list': 0.33; 'skip:" 20': 0.33; 'there': 0.35; 'skip:@ 10': 0.35; 'else': 0.35; 'reference': 0.35; 'certain': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'could': 0.38; 'but': 0.38; 'subject:: ': 0.38; '8bit%:6': 0.39; 'doing': 0.39; 'should': 0.39; 'received:209': 0.39; 'correctly': 0.39; 'to:addr:python.org': 0.39; 'really': 0.40; 'more': 0.60; 'order': 0.62; 'capable': 0.64; 'further': 0.65; 'cause': 0.67; 'trick.': 0.84; 'confusion.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=uJ8ZIh53PiZ1jdSz7ctc9VjYaw5EElRE+4QooK8+fVE=; b=ah/R8oZb/7zg6NYBl4yEt0bFmKd/rvO18vArdDb07IHHQ6pfNzFCrfLK3FGPYSXV7T e3z3xKUwmdPdlpwbGDyScPHceifP7EMam6rEGAk5NKiqmPQ9fJNiGNgD9houZcN8dRIk rr5yeXhf3ff4uINScFPIGioRjcGiajmaTsasE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=fCluoKIQ0/5dvrClb6wSWBY1Q5S68EnZE/07455OQBxuKTdkB5jraKmSX2xZIJcAXG ER0pxGEi4WJDByGS065IXehqXWWhbAK42Bj37buZnknZcFhJuWBf/ULwnYgDd8u3GbdB fkypoDOb5Z+XMh0uO8oUAUOtmjcHdMWn6Yotg= MIME-Version: 1.0 In-Reply-To: References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> From: Ian Kelly Date: Fri, 3 Jun 2011 11:53:05 -0600 Subject: Re: Something is rotten in Denmark... To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 67 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307123618 news.xs4all.nl 49175 [::ffff:82.94.164.166]:48987 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6958 On Fri, Jun 3, 2011 at 2:30 AM, Thomas Rachel wrote: > So there should be a way to replace the closure of a function with a > snapshot of it at a certain time. If there was an internal function with > access to the readonly attribute func_closure and with the capability of > changing or creating a cell object and thus hbeing capable of doing so, i= t > could be used a a decorator for a function to be "closure-snapshotted". > > So in > > funcs=3D[] > for i in range(100): > =A0@closure_snapshot > =A0def f(): return i > =A0funcs.append(f) > > each f's closure content cells would just be changed not to point to the > given variables, but to a cell referenced nowhere else and initialized wi= th > the reference pointed to by the original cells at the given time. For CPython 3.2: import functools import types def makecell(value): def f(): return value return f.__closure__[0] def closure_snapshot(f): if f.__closure__: snapshot =3D tuple(makecell(cell.cell_contents) for cell in f.__clo= sure__) else: snapshot =3D f.__closure__ g =3D types.FunctionType(f.__code__, f.__globals__.copy(), f.__name__, f.__defaults__, snapshot) functools.update_wrapper(g, f, functools.WRAPPER_ASSIGNMENTS + ('__kwdefaults__',)) return g >>> funcs =3D [] >>> for i in range(10): ... @closure_snapshot ... def f(): return i ... funcs.append(f) ... >>> [f() for f in funcs] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> funcs =3D [closure_snapshot(lambda: i) for i in range(10)] >>> [f() for f in funcs] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] It doesn't really seem any more straightforward to me than the "i=3Di" trick. Also, I don't know how portable this is to different Python implementations or future versions. Finally, note that in order to make this work correctly in all cases (such as the first example above, where i is a global, not a cell) we have to snapshot the globals as well, which could cause further confusion. Cheers, Ian