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


Groups > comp.lang.python > #12142

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'assign': 0.04; 'parameter': 0.05; 'python': 0.08; 'decorator': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'referenced': 0.09; 'skip:[ 30': 0.09; 'statement.': 0.09; 'def': 0.15; "'a'": 0.16; 'adam': 0.16; 'decorators.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:function': 0.16; 'wrote:': 0.16; '>>>': 0.18; '(most': 0.21; "doesn't": 0.22; 'variable': 0.24; 'traceback': 0.24; 'parameters': 0.25; "i'm": 0.27; 'work.': 0.27; 'all,': 0.28; 'effect': 0.28; 'fix': 0.29; 'from:addr:web.de': 0.30; "didn't": 0.31; 'actual': 0.32; 'to:addr:python-list': 0.33; 'assignment': 0.34; 'function.': 0.34; 'last):': 0.34; 'weird': 0.34; 'header:X -Complaints-To:1': 0.35; 'file': 0.36; 'issue': 0.36; 'another': 0.37; 'variables': 0.37; 'two': 0.37; 'think': 0.38; 'received:org': 0.38; 'some': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'subject:with': 0.39; 'to:addr:python.org': 0.39; 'skip:d 20': 0.39; "it's": 0.40; 'experiencing': 0.84; 'illustrated': 0.84; 'refuses': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data...
Date Wed, 24 Aug 2011 15:29:58 +0200
Organization None
References <CAMX1RokbXLL=typKkOE3HLpxcihJGfnSLkcivyLZypXqgvj1fg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a576.dip.t-dialin.net
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.390.1314192603.27778.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314192603 news.xs4all.nl 23977 [2001:888:2000:d::a6]:50934
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12142

Show key headers only | View raw


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.

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


Thread

Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data... Peter Otten <__peter__@web.de> - 2011-08-24 15:29 +0200

csiph-web