[Python-Dev] statically nested scopes

M.-A. Lemburg mal@lemburg.com
Fri, 03 Nov 2000 13:50:17 +0100

"M.-A. Lemburg" wrote:
> Jeremy Hylton wrote:
> >
> > Looks like we need to rehash this thread at least enough to determine
> > who is responsible for causing us to rehash it.
> >
> > MAL said it would break code.  I asked how.  Skip and Tim obliged with
> > examples.  I said their examples exhibited bad style; neither of them
> > claimed they were good style.
> >
> > In the end, I observed that while it could break code in theory, I
> > doubted it really would break much code.  Furthermore, I believe that
> > the code it will break is already obscure so we needn't worry about
> > it.
> That's just what I was trying to say all along: statically
> nested scopes don't buy you anything except maybe for lambdas
> and nested functions (which is bad style programming, IMHO too).
> The only true argument for changing scoping I see is that
> of gained purity in language design... without much practical
> use.
> Other issues that need sorting out:
> x = 2
> class C:
>   x = 1
>   C = 'some string'
>   def a(self):
>      print x
>   def b(self):
>      global x
>      x = 3
> class D(C):
>   C = 'some string'
>   def a(self):
>      C.a(self)
>      print C
> o = C()
> o.a()
> o.b()
> o.a()
> o = D()
> o.a()
> What would the output look like under your proposal ?

[Moshe pointed out to me in private mail that the above would
continue to work as it does now due to a difference being made
between class and function scoping categories]

More questions:

How are you going to explain the different
scoping categories to a newbie ? What if you define a class 
within a method ? How can you explicitely attach a dynamically
defined class to a certain scope ?

More problems (?!):

Nested scopes will introduce cycles in all frame objects.
This means that with GC turned off, frame objects will live
forever -- Python will eat up memory at a very fast pace.

BTW, Python's GC only works for a few builtin types (frames
are not among the supported types): what if a frame finds its
way into a user defined type ? Will GC still be able to cleanup
the cycles ?

Perhaps I'm just being silly, but I still don't see the
benefits of breaking todays easy-to-grasp three level
scoping rules... 

Marc-Andre Lemburg
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/