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


Groups > comp.lang.python > #12144

Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data...

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 <adam.jorgensen.za@gmail.com>
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 <j32uc8$b8k$1@dough.gmane.org>
References <CAMX1RokbXLL=typKkOE3HLpxcihJGfnSLkcivyLZypXqgvj1fg@mail.gmail.com> <j32uc8$b8k$1@dough.gmane.org>
From Adam Jorgensen <adam.jorgensen.za@gmail.com>
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 <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.392.1314196255.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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):
>>     def decorate(func):
>>         argspec = getfullargspec(func)
>>         decode_args = [argspec.args.index(decode_arg) for decode_arg
>> in decode_args]
>
> I didn't read the whole thing, but:
>
>>>> def f(a):
> ...     def g():
> ...             a = a + 42
> ...             return a
> ...     return g
> ...
>>>> f(1)()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "<stdin>", line 3, in g
> UnboundLocalError: local variable 'a' referenced before assignment
>
> Python treats variables as local if you assign a value to them anywhere in
> the function. The fix is easy, just use another name:
>
>>>> def f(a):
> ...     def g():
> ...             b = a + 42
> ...             return b
> ...     return g
> ...
>>>> f(1)()
> 43
>
> In Python 3 you can also use the nonlocal statement.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data... Adam Jorgensen <adam.jorgensen.za@gmail.com> - 2011-08-24 16:30 +0200
  Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-25 10:54 +1000

csiph-web