Re: Last message ( Scope & globals & binding )

Steven D. Majewski (sdm7g@aemsun.med.Virginia.EDU)
Mon, 9 Dec 91 03:48:03 EST

> From guido@cwi.nl Sat Dec 7 06:10:51 1991
>
> You are figuring out the obscure parts of Python at an amazing
> speed... You actually discovered something that I can't explain!
> Indeed the documentation isn't very clear about globals except for
> that one passage in misc/CLASSES.
>
> ...
>
> I also thought that local variables would be in the
> majority, and disliked the possibility that using a local variable
> like "i" would accidentally clash with a global also named "i".
> Therefore I decided that all variable assignments were to be local.
>
> ...
>
> You're right that a way of faking global variables is to lock them all
> up in a class instance, and there are several demo programs that use
> this trick (e.g., mclock). Object-oriented design can usually avoid
> the need for global variables in the first place.
>
> But I still think that for simple programs the global variable
> assignment has the merit of simplicity and relative elegance, and
> actually I am thinking about introducing a 'global' statement with
> which you can declare that names you are using in a function are
> actually references to globals. This statement would be placed
> inside the function body that uses the globals. Python's main source
> of inspiration, ABC, has a similar statement (SHARE), and I think I
> saw it in one of its competitors (Tcl) as well. (What's Perl's
> attitude towards locals and globals? I forgot...)
>

I agree that for a language that will be used for quick programs
( and quick fixes/kludges ) globals are probably essential.

The problem wasn't hard to find. ( Or maybe I should say the problem
wasn't hard to GENERATE - I did have a little trouble FINDING it. )
I was just throwing in one of those quick kludges and adding a global
variable was quicker than rethinking the module interfaces.

However, the process of fixing it ( my program ) left me a bit ambivalent.
I an definitely not a proponent of "straight-jacket" program languages
that make it impossible to do many things in order to keep you from
doing ONE BAD thing. But I'm not against a language that somwhow has
features that encourage you to write GOOD code.

The first fix was that I wrote my globals with "class Global(): pass"
null classes. However, once I got the original to work, I was drawn
to try to OO it a bit more.
I had globals: initial_depth, max_depth and local current_depth, plus
function dir_depth ( just counted the number of "/" in a pathname ).
I now have a half-asses OO version that was just bad enough to show
me the RIGHT way to do it. So I found trying to do without globals
a good learning experience. But probably not one I want to experience
when I'm in a big hurry to "get the damn thing working!"

Anyway:
"global a = 0"
"global b,c "
Can easily be the syntactic shorthand for:
Class __Global__():
pass
__global__ = __Global__()
__global__.a = 0
__global__.b =
__global__.c =

Global to the current module of course.
But access to module.a would be equivalent to module.__global__.a
[ I am here assuming an addition to the symbol table search path. ]

[ BTW Guido - have you ever looked at FORTH dictionaries ?
The syntax of alternate namespaces is awkward in FORTH, but
the dictionaries are tree structured, with the search going
backward from the local leaves to the root ( with side trips
programmable ) so the use of the dictionary search order is
the primary way of managing the namespace. ]

What to do with b,c - that's the rub. Ideally, we would like to
catch the error of access of uninitialized variables. i.e. using
the global value 'b' before an assignment to b should be signaled.
Is there an easy way for it to be know to be a global name and yet
be unbound ?

- Steve

( Again: sorry about the lack of discipline due to the late hour and
a slow modem at home. I meant to indicate that I agreed with the
comment I included above: preponderence of local ref. over global
and of global read ref. over global write ref. , etc. )

======== "If you have a hammer, find a nail" - George Bush,'91 =========
Steven D. Majewski University of Virginia Physiology Dept.
sdm7g@Virginia.EDU Box 449 Health Sciences Center
Voice: (804)-982-0831 1600 Jefferson Park Avenue
FAX: (804)-982-1616 Charlottesville, VA 22908