[Tutor] Notes on namespaces, scopes, etc

Dave Kuhlman dkuhlman at rexx.com
Wed Aug 2 23:43:33 CEST 2006


On Fri, Jul 28, 2006 at 02:36:08PM -0700, Danny Yoo wrote:
> > If there were really such a thing as nested scopes/namespaces, we would 
> > have a function that would give us access to them, similar to the way 
> > that locals() and globals() give us access to the local and global 
> > namespace.
> 
> It would be _convenient_ to have such a function for inspection, but it's 
> not a requirement.
> 
> Here's a function that shows lexical scope in action:
> 
> ##########################
> >>> def pair(x, y):
> ...     def f(b):
> ...         if b: return x
> ...         return y
> ...     return f
> ...
> >>> p = pair(3, 4)
> >>> p(True)
> 3
> >>> p(False)
> 4
> ##########################

I believe that I've incorporated most, if not all, of the
suggestions from those on the list, except for Danny's suggestion
(above) about closures.  To paraphrase "The Treasure of the Sierra
Madre":

    "Closures?  Closures?  We don't got to show you no stinking
    closures."

After all, these notes on notes and bindings were intended to
beginners, not computer science students.  But, I did add a link to
an explanation of closures.

I've also added a collection of links to related material at the
end of these notes.  Hopefully, if my explanation is confusing,
Alan's explanation or the standard docs will pull them through.

The document itself is still here:

    http://www.rexx.com/~dkuhlman/python_comments.html#namespaces

And, thanks again for all the help.

Off topic -- For those of you who need the quote:

    "Badges? We ain't got no badges. We don't need no badges.  I
    don't have to show you any stinking badges!"
      --Gold Hat, as played by Alfonso Bedoya
      "The Treasure of the Sierra Madre" (1948)

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list