Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12144 > unrolled thread
| Started by | Adam Jorgensen <adam.jorgensen.za@gmail.com> |
|---|---|
| First post | 2011-08-24 16:30 +0200 |
| Last post | 2011-08-25 10:54 +1000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Adam Jorgensen <adam.jorgensen.za@gmail.com> |
|---|---|
| Date | 2011-08-24 16:30 +0200 |
| Subject | Re: Weird interaction with nested functions inside a decorator-producing function and closuring of outer data... |
| Message-ID | <mailman.392.1314196255.27778.python-list@python.org> |
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 >
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-25 10:54 +1000 |
| Message-ID | <4e559d60$0$29989$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #12144 |
Adam Jorgensen wrote: > Thanks :-) Sorry about the size, I wasn't sure what was relevant... We prefer that you don't top-post here, because it makes it hard to see context when people reply. In general, people asking questions should always try to reduce the problem to the simplest code that will demonstrate the problem. This has two huge advantages: (1) We're all volunteers here, none of us are paid to solve your problems. The more work needed to answer a question that we might not care that much about, the less likely it is that you will get good answers. The easier you make it for us, the more likely you will get many good, prompt answers. (2) Even more important... the process of cutting out all the irrelevant details and reducing the code to the smallest possible example may reveal to you what the problem is. As the old proverb goes, "solve a coder's problem, and you've solved one problem... teach a coder how to solve his own problems, and you've solved them all" *wink* I can't begin to count how many times I've started writing up a post to ask a question, and in the process of reducing the example code to the smallest it can be, I've solved it myself. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web