[Python-Dev] Using and binding relative names (was Re: PEP forBetter Control of Nested Lexical Scopes)
Ron Adam
rrr at ronadam.com
Mon Feb 27 01:20:07 CET 2006
Alex Martelli wrote:
> On Feb 26, 2006, at 11:47 AM, Ron Adam wrote:
> ...
>> How would you know you aren't in inadvertently masking a name in a
>> function you call?
>
> What does calling have to do with it? Nobody's proposing a move to
> (shudder) dynamic scopes, we're talking of saner concepts such as
> lexical scopes anyway. Can you give an example of what you mean?
(sigh of relief) Ok, so the following example will still be true.
def foo(n): #foo is a global
return n
def bar(n):
return foo(n) #call to foo is set at compile time
def baz(n):
foo = lambda x: 7 #will not replace foo called in bar.
return bar(n)
print baz(42)
I guess I don't quite get what they are proposing yet.
It seems to me adding intermediate scopes are making functions act more
like class's. After you add naming conventions to functions they begin
to look like this.
""" Multiple n itemiter """
class baz(object):
def getn(baz, n):
start = baz.start
baz.start += n
return baz.lst[start:start+n]
def __init__(baz, lst):
baz.lst = lst
baz.start = 0
b = baz(range(100))
for n in range(1,10):
print b.getn(n)
> For the record: I detest the existing 'global' (could I change but ONE
> thing in Python, that would be the one -- move from hated 'global' to a
> decent namespace use, e.g. glob.x=23 rather than global x;x=23), and I'd
> detest a similar 'outer' just as intensely (again, what I'd like instead
> is a decent namespace) -- so I might well be sympathetic to your POV, if
> I could but understand it;-).
Maybe something explicit like:
>>> import __main__ as glob
>>> glob.x = 10
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'glo
b': <module '__main__' (built-in)>, '__doc__': None, 'x': 10}
>>>
That could eliminate the global keyword.
I'm -1 on adding the intermediate (outer) scopes to functions. I'd even
like to see closures gone completely, but there's probably a reason they
are there. What I like about functions is they are fast, clean up
behind themselves, and act *exactly* the same on consecutive calls.
Cheers,
Ron
More information about the Python-Dev
mailing list