Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assign': 0.04; 'parameter': 0.05; ':-)': 0.06; 'python': 0.08; '>>>>': 0.09; 'decorator': 0.09; 'referenced': 0.09; 'skip:[ 30': 0.09; 'statement.': 0.09; 'def': 0.15; "'a'": 0.16; 'adam': 0.16; 'decorators.': 0.16; 'subject: \n ': 0.16; 'subject:function': 0.16; 'wrote:': 0.16; '(most': 0.21; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'variable': 0.24; 'traceback': 0.24; 'parameters': 0.25; "i'm": 0.27; 'work.': 0.27; 'all,': 0.28; 'effect': 0.28; "wasn't": 0.28; 'url:mailman': 0.28; 'message- id:@mail.gmail.com': 0.29; 'fix': 0.29; 'thanks': 0.30; "didn't": 0.31; 'actual': 0.32; 'to:addr:python-list': 0.33; 'url:listinfo': 0.33; '...': 0.34; 'assignment': 0.34; 'function.': 0.34; 'last):': 0.34; 'weird': 0.34; 'url:python': 0.36; 'issue': 0.36; 'another': 0.37; 'variables': 0.37; 'two': 0.37; 'think': 0.38; 'size,': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'subject:with': 0.39; 'to:addr:python.org': 0.39; 'received:74.125': 0.39; 'skip:d 20': 0.39; "it's": 0.40; 'august': 0.70; 'experiencing': 0.84; 'illustrated': 0.84; 'otten': 0.84; 'refuses': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=B8ATDQW28ZZInEKiHcH31c+63GF/P3K0Qj5kKBRA++0=; b=wcGfnX6HfgaF1XOW+5y1bJEQQc392Ns3xYzzxRWtCAjwkXE98/nLdaRMCdXfctRE6H XhEdzJO4mugolL84RJTozPmSCTCFYtORXfTHVuZmz7shHb6QEOaE+Z6EMSEzGGw2n58L O99L7wZq/jU78CV2Kf9jx1W/ueT62UiiAh/kw= MIME-Version: 1.0 In-Reply-To: References: From: Adam Jorgensen Date: Wed, 24 Aug 2011 16:30:13 +0200 Subject: Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data... 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314196264 news.xs4all.nl 23892 [2001:888:2000:d::a6]:34282 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12144 Thanks :-) Sorry about the size, I wasn't sure what was relevant... On 24 August 2011 15:29, Peter Otten <__peter__@web.de> wrote: > Adam Jorgensen wrote: > >> Hi all, I'm experiencing a weird issue with closuring of parameters >> and some nested functions I have inside two functions that >> return decorators. I think it's best illustrated with the actual code: > > You should have made an effort to reduce its size >> # This decorator doesn't work. For some reason python refuses to >> closure the *decode_args parameter into the scope of the nested >> decorate and decorate_with_rest_wrapper functions >> # Renaming *decode_args has no effect >> def rest_wrapper(*decode_args, **deco_kwargs): >> =A0 =A0 def decorate(func): >> =A0 =A0 =A0 =A0 argspec =3D getfullargspec(func) >> =A0 =A0 =A0 =A0 decode_args =3D [argspec.args.index(decode_arg) for deco= de_arg >> in decode_args] > > I didn't read the whole thing, but: > >>>> def f(a): > ... =A0 =A0 def g(): > ... =A0 =A0 =A0 =A0 =A0 =A0 a =3D a + 42 > ... =A0 =A0 =A0 =A0 =A0 =A0 return a > ... =A0 =A0 return g > ... >>>> f(1)() > Traceback (most recent call last): > =A0File "", line 1, in > =A0File "", line 3, in g > UnboundLocalError: local variable 'a' referenced before assignment > > Python treats variables as local if you assign a value to them anywhere i= n > the function. The fix is easy, just use another name: > >>>> def f(a): > ... =A0 =A0 def g(): > ... =A0 =A0 =A0 =A0 =A0 =A0 b =3D a + 42 > ... =A0 =A0 =A0 =A0 =A0 =A0 return b > ... =A0 =A0 return g > ... >>>> f(1)() > 43 > > In Python 3 you can also use the nonlocal statement. > > -- > http://mail.python.org/mailman/listinfo/python-list >