Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; '(python': 0.05; 'modify': 0.05; 'none:': 0.05; '(using': 0.07; 'works.': 0.07; 'python': 0.09; 'assigning': 0.09; 'yeah,': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; '2.7.3': 0.16; 'cleaner': 0.16; 'did,': 0.16; 'distinct': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'list),': 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; 'usually.': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'variable': 0.20; '3.x': 0.22; 'assignment': 0.22; 'explicit': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; "i've": 0.23; 'seems': 0.23; 'somewhere': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:m 30': 0.26; '(e.g.': 0.27; 'right.': 0.27; 'i.e.': 0.27; 'container': 0.29; 'omitted': 0.29; 'yes.': 0.29; 'case,': 0.29; 'probably': 0.29; 'function': 0.30; 'good.': 0.32; "can't": 0.34; 'done': 0.34; 'thanks': 0.34; 'needed': 0.35; 'path': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'there': 0.35; 'but': 0.36; 'received:au': 0.36; 'subject:with': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'level': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'think': 0.40; 'john': 0.60; 'content- disposition:inline': 0.60; 'skip:u 10': 0.60; 'kind': 0.61; 'subject:...': 0.63; 'occasion': 0.84 Date: Mon, 15 Oct 2012 12:39:18 +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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350265161 news.xs4all.nl 6849 [2001:888:2000:d::a6]:45865 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31273 On 14Oct2012 19:27, Ian Kelly wrote: | On Sun, Oct 14, 2012 at 7:08 PM, Cameron Simpson wrote: | > Is attr_name omitted from locals() in made_file_property _because_ I | > have an assignment statement? | | Yes. Syntactically, a variable is treated as local to a function if | it is assigned to somewhere in that function and there is no explicit | global or nonlocal declaration. Aha. Good. | > 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): [...] | > if attr_name is None: | > my_attr_name = '_' + func.__name__ | > else: | > my_attr_name = attr_name [...] | > 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? | | That's a perfectly good way to do it as long as you don't want to | actually change the value of the outer attr_name. Well, I don't need to - using a distinct local variable will do the job. I just hadn't realised I needed the extra level of naming. | If you did, then | you would either declare the variable as nonlocal (Python 3.x only) ... which is why I couldn't find such in the 2.7.3 doco ... | or | use a container (e.g. a 1-element list), which would allow you to | modify the contents of the container without actually assigning to the | variable. Ah. Yeah, tacky; I've done that kind of thing in the past on occasion but using a distinct local name is much cleaner here, and probably usually. | > 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. | | Exactly right. Thanks for the explaination. Now I know a New Thing. Cheers, -- Cameron Simpson "Vy can't ve chust climb?" - John Salathe