[Python-Dev] Using and binding relative names (was Re: PEP forBetter Control of Nested Lexical Scopes)

Alex Martelli aleaxit at gmail.com
Mon Feb 27 01:55:21 CET 2006


On Feb 26, 2006, at 4:20 PM, Ron Adam wrote:
    ...
> (sigh of relief) Ok, so the following example will still be true.

Yep, no danger of dynamic scoping, be certain of that.

> Maybe something explicit like:
>
>>>> import __main__ as glob

Sure, or the more general ''glob=__import__(__name__)''.

> 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.

Except that the latter assertion is just untrue in Python -- we  
already have a bazilion ways to perform side effects, and, since  
there is no procedure/function distinction, side effects in functions  
are an extremely common thing.  If you're truly keen on having the  
"exactly the same" property, you may want to look into functional  
languages, such as Haskell -- there, all data is immutable, so the  
property does hold (any *indispensable* side effects, e.g. I/O, are  
packed into 'monads' -- but that's another story).

Closures in Python are often extremely handy, as long as you use them  
much as you would in Haskell -- treating data as immutable (and in  
particular outer names as unrebindable). You'd think that functional  
programming fans wouldn't gripe so much about Python closures being  
meant for use like Haskell ones, hm?-)  But, of course, they do want  
to have their closure and rebind names too...


Alex



More information about the Python-Dev mailing list