On 14Oct2012 19:27, Ian Kelly <ian.g.ke...@gmail.com> wrote: | On Sun, Oct 14, 2012 at 7:08 PM, Cameron Simpson <c...@zip.com.au> 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 <c...@zip.com.au> "Vy can't ve chust climb?" - John Salathe -- http://mail.python.org/mailman/listinfo/python-list