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


Groups > comp.lang.python > #31264

Re: trouble with nested closures: one of my variables is missing...

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
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; 'none:': 0.05; 'sys': 0.05; '21,': 0.07; 'assign': 0.07; 'python': 0.09; 'referenced': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'closures': 0.16; 'declaration': 0.16; 'declaration,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:edu.au': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'simpson': 0.16; 'stripped': 0.16; 'worries.': 0.16; 'wrote:': 0.17; 'obviously': 0.18; 'variable': 0.20; 'skip:" 30': 0.20; 'import': 0.21; 'error.': 0.21; 'assignment': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'this:': 0.23; "i've": 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'looks': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.26; '(most': 0.27; 'mind.': 0.27; 'skip:> 10': 0.27; 'chris': 0.28; 'trouble': 0.28; 'thinks': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'function': 0.30; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'defining': 0.33; 'traceback': 0.33; "can't": 0.34; 'something': 0.35; 'but': 0.36; 'received:au': 0.36; 'subject:with': 0.36; 'charset:us-ascii': 0.36; 'october': 0.37; 'does': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'your': 0.60; 'content-disposition:inline': 0.60; 'skip:u 10': 0.60; 'back': 0.62; 'subject:...': 0.63; 'here': 0.65; 'lack': 0.71; 'pardon': 0.84
Date Mon, 15 Oct 2012 08:54:02 +1100
From Cameron Simpson <cs@zip.com.au>
To Chris Rebert <clp2@rebertia.com>
Subject Re: trouble with nested closures: one of my variables is missing...
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <CAMZYqRRyUp5oWW3Ap4RXes7yn_gCx-PGkJyfijsFtru-CLcEww@mail.gmail.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <CAMZYqRRyUp5oWW3Ap4RXes7yn_gCx-PGkJyfijsFtru-CLcEww@mail.gmail.com>
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2182.1350251654.27098.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350251654 news.xs4all.nl 6903 [2001:888:2000:d::a6]:44482
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31264

Show key headers only | View raw


On 13Oct2012 22:07, Chris Rebert <clp2@rebertia.com> wrote:
| On Saturday, October 13, 2012, Cameron Simpson wrote:
| > I'm having some trouble with closures when defining a decorator.
| <snip>
| 
| > However, I can't make my make_file_property function work. I've stripped
| > the code down and it does this:
| <snip>
| 
| >   Traceback (most recent call last):
| >     File "foo.py", line 21, in <module>
| >       def f(self, foo=1):
| >     File "foo.py", line 4, in file_property
| >       return make_file_property()(func)
| >     File "foo.py", line 10, in made_file_property
| >       if attr_name is None:
| >   UnboundLocalError: local variable 'attr_name' referenced before
| > assignment
| >
| > Observe above that 'unset_object' is in locals(), but not 'attr_name'.
| > This surprises me.
| >
| > The stripped back code (missing the internals of the file property
| > watcher) looks like this:
| >
| >   import sys
| >
| >   def file_property(func):
| >     return make_file_property()(func)
| >
| >   def make_file_property(attr_name=None, unset_object=None, poll_rate=1):
| >     print >>sys.stderr, "make_file_property(attr_name=%r, unset_object=%r,
| > poll_rate=%r): locals()=%r" % (attr_name, unset_object, poll_rate,locals())
| >     def made_file_property(func):
| 
| You're missing a "nonlocal" declaration here.
| 
|       print >>sys.stderr, "made_file_property(func=%r): locals()=%r" %
| > (func, locals())
| >       if attr_name is None:
| >         attr_name = '_' + func.__name__
| 
| 
|  You assign to it, but there's no nonlocal declaration, so Python thinks
| it's a local var, hence your error.

But 'unset_object' is in locals(). Why one and not the other?
Obviously there's something about closures here I'm missing.

| Pardon my brevity and some lack of trimming; I'm on a smartphone and in a
| rush.

No worries. Thansk for the rpely.
-- 
Cameron Simpson <cs@zip.com.au>

A clean desk is the sign of a blank mind.

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


Thread

Re: trouble with nested closures: one of my variables is missing... Cameron Simpson <cs@zip.com.au> - 2012-10-15 08:54 +1100

csiph-web