nested scopes

D-Man dsh8290 at rit.edu
Tue Feb 6 09:38:09 EST 2001


On Mon, Feb 05, 2001 at 06:39:07PM +0000, Darren New wrote:
| D-Man wrote:
| > professor didn't know of any situations where it would be
| > advantageous, and I couldn't contrive any either.  I was kind of
| > surprised when I heard python described as "dynamically scoped"
| > because its scoping was really quite natural for me.
| 
| That's because your prof doesn't know what "dynamic scoping" is.

Maybe not,  it seemed very confusing.  I still have the book, though I
haven't read it much (maybe I should ...)

Do you know of any situations where it would be advantageous to have
dynamic scoping instead of static scoping?  (Note that I'm not asking
for situations where dynamic scoping exists, but where it would be
better)

| 
| 
| def alpha():
|   a = 5
|   gamma()
|   beta()
|   gamma()
| 
| def beta():
|   a = 17
|   gamma()
| 
| def gamma():
|   print a
| 
| alpha()
| 
| With dynamic scoping, this would print "5" then "17" then "5" again, as
| "gamma" would basically walk up the run-time call stack looking for the most
| recent "a" to print. (Note that "a" is supposed to be local everywhere, not

Yeah, this is what I thought dynamic scoping would do.

| a global.) Obviously, this is *not* how Python works.  

Right, in Python, gamma() would complain that it can't find 'a'.  This
is why I said that python's scope rules are really not hard to
understand.

-D





More information about the Python-list mailing list