In defence of the two-namespace rule

Edward Welbourne eddyw at lsl.co.uk
Thu Jan 20 09:39:31 EST 2000


Markus Stenberg and Bernhard Herzog consider the two-namespace rule to
be a deficiency in python, and think it would be `simpler' to use
`lexical scoping'.  The Tim has given an excellent and erudite summary
of what that could be asked to mean, since Evan asked and its advocates
didn't answer.  Personally, I consider the two-namespace rule to be a
major *strength* of python: it is a major ingredient in pythonicness.

Define a function, foo, which returns a function it has defined, as bar,
during its suite; have bar refer to some of the local variables of foo.
With lexical scoping, the interpreter is obliged to keep the namespace
foo used (on that invocation) for as long as anyone retains a reference
to what it returned.

I contend that the namespace in which a function executes its suite
*should* be discardable at the time the function returns.  Lexical
scoping would mean that we can't discard foo's namespace when foo
finishes execution.  The implications of this for garbage collection are
singularly unpleasant - give them some thought.

The only way out is for the values bar accesses from foo to be
transcribed, before foo finishes executing, into some namespace whose
lifespan is at least as long as that of bar (and, ideally, coeval with
it).  Since bar carries around a namespace, for its defaults, with
exactly the right life-span, that's the right place to which to
transcribe such values (promoting them to globals being, I trust, to
no-one's liking).  Now, some folk might prefer to have that done
implicitly: personally, I'm all in favour of each suite of code needing
to be explicit about its dependencies on others.  Like Evan,

> I rather like the idea of having to explicitly bring names into an
> inner scope, and with semantics identical to parameter defaults.

So, yes, this is a matter of taste: the two-namespace rule is an emetic
to some, to whom I would commend Algol as a language to use instead of
python; but to pythoneers, the two-namespace rule is harmonious,
beautiful and kind to the garbage-collector.  Treat the humblest of thy
servants kindly, lest they rise up and smite thee with just zeal.

> when-guido-sez-"too-radical"-he-means-"not-radical-
>     enough"-ly y'rs  - tim
Ach, so back-pedalling would be a wrong strategy ?
I'll bear that in mind - thanks for the advice ;^>

	Eddy.




More information about the Python-list mailing list