
Skip Montanaro <skip@pobox.com> writes:
John> How about (to abuse a keyword that's gone unmolested for too long)
John> global foo from def
John> to declare that foo refers a variable in a lexically enclosing John> function definition? This avoids to need to name a specific John> function (which IMHO is just a source of confusion over the John> semantics of strange cases) while still having some mnemonic value John> (foo "comes from" an enclosing function definition).
How do you indicate the particular scope to which foo will be bound (there can be many lexically enclosing function definitions)? Using my example again:
def outer(a): x = a def inner(a): x = 42 def innermost(r): global x from def # <--- your notation x = r print " inner, x @ start:", x innermost(random.random()) print " inner, x @ end:", x print "outer, x @ start:", x inner(a) print "outer, x @ end:", x
how do you tell Python that x inside innermost is to be associated with the x in inner or the x in outer?
Maybe "global foo from <function_name>" ? Or, "from function_name global foo" is consistent with import, albeit somewhat weird. I would never use this feature; I avoid nested functions entirely. However, as long as we're talking about this stuff, I wish I could write "global foo" at module scope and have that mean "this variable is to be treated as global in all functions in this module". zw