Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.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: 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; 'else:': 0.03; 'none:': 0.05; 'that?': 0.05; '(using': 0.07; 'assign': 0.07; 'parameter': 0.07; 'variable,': 0.07; 'works.': 0.07; 'python': 0.09; 'assigning': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'yet.': 0.13; 'value.': 0.15; 'closures': 0.16; 'declaration,': 0.16; 'distinct': 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; 'oct': 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; 'wrote:': 0.17; 'obviously': 0.18; 'variable': 0.20; 'skip:" 30': 0.20; 'bit': 0.21; 'error.': 0.21; 'assignment': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.26; 'i.e.': 0.27; 'skip:> 10': 0.27; 'omitted': 0.29; 'thinks': 0.29; 'case,': 0.29; 'included': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'function': 0.30; 'up.': 0.31; 'code': 0.31; 'point': 0.31; 'print': 0.32; "can't": 0.34; 'path': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'received:au': 0.36; 'subject:with': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'think': 0.40; 'your': 0.60; 'content- disposition:inline': 0.60; 'skip:u 10': 0.60; 'free': 0.61; 'subject:...': 0.63; 'here': 0.65; 'locals': 0.84 Date: Mon, 15 Oct 2012 12:08:40 +1100 From: Cameron Simpson To: Ian Kelly 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: User-Agent: Mutt/1.5.21 (2010-09-15) References: Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350263323 news.xs4all.nl 6845 [2001:888:2000:d::a6]:37672 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31271 On 14Oct2012 18:32, Ian Kelly wrote: | On Sun, Oct 14, 2012 at 3:54 PM, Cameron Simpson wrote: | > | 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. | | 'unset_object' is in locals because it's a free variable and those are | included in locals(), and it has a value. | | 'attr_name' is not in locals because while it's a local variable, it | has not been assigned to yet. It has no value and an attempt to | reference it at that point would result in an UnboundLocalError. Can you elaborate a bit on that? The only place in my code that unset_object is set is as a default parameter in make_file_property (snippet): 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): print >>sys.stderr, "made_file_property(func=%r): locals()=%r" % (func, locals()) if attr_name is None: attr_name = '_' + func.__name__ and attr_name is set there also. Is attr_name omitted from locals() in made_file_property _because_ I have an assignment statement? If that's the case, should I be doing this (using distinct names for the closure variable and the function local variable): 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): print >>sys.stderr, "made_file_property(func=%r): locals()=%r" % (func, locals()) if attr_name is None: my_attr_name = '_' + func.__name__ else: my_attr_name = attr_name lock_name = my_attr_name + '_lock' def getprop(self): with getattr(self, lock_name): pass return getattr(self, my_attr_name, unset_object) i.e. deliberately _not_ assigning to attr_name as as to _avoid_ masking the outer attr_name from the inner locals()? BTW, doing that works. Is that The True Path for this situation? If so, I think I now understand what's going on: Python has inspected the inner function and not placed the outer 'attr_name' into locals() _because_ the inner function seems to have its own local attr_name in use, which should not be pre-tromped. -- Cameron Simpson Nothing is so smiple that it can't get screwed up.