Python's Lisp heritage

Carl Banks imbosol at vt.edu
Mon Jul 15 18:28:30 EDT 2002


Christopher Browne wrote:
> Dynamic scope buys you the ability to not need to specify _all_ the
> values that you might be customizing.

As does lexical.


> If a new Emacs mode requires adding in a bunch of additional
> parameters, dynamic scope lets them be visible throughout the scope
> during which they are "live" as opposed to just within the environment
> in which they were defined.

You can put all the defuns inside a let form.


> From a paper on Emacs:
> 
>  "Some language designers believe that dynamic binding should be
>  avoided, and explicit argument passing should be used
>  instead. Imagine that function A binds the variable FOO, and calls
>  the function B, which calls the function C, and C uses the value of
>  FOO. Supposedly A should pass the value as an argument to B, which
>  should pass it as an argument to C.
> 
>  This cannot be done in an extensible system, however, because the
>  author of the system cannot know what all the parameters will
>  be. Imagine that the functions A and C are part of a user extension,
>  while B is part of the standard system. The variable FOO does not
>  exist in the standard system; it is part of the extension. To use
>  explicit argument passing would require adding a new argument to B,
>  which means rewriting B and everything that calls B. In the most
>  common case, B is the editor command dispatcher loop, which is
>  called from an awful number of places.
> 
>  What's worse, C must also be passed an additional argument. B
>  doesn't refer to C by name (C did not exist when B was written). It
>  probably finds a pointer to C in the command dispatch table. This
>  means that the same call which sometimes calls C might equally well
>  call any editor command definition. So all the editing commands must
>  be rewritten to accept and ignore the additional argument. By now,
>  none of the original system is left!"
> 
> If all you have is lexical scoping, parameters have to get explicitly
> passed down the chain in order to get from function A to function C.
> That means introducing additional parameters to function B, which
> didn't actually care about those extra values.

(let ((foo whatever))
  (defun A (x)
     (setq foo x)
     (B (symbol-function C)))
  (defun C ()
     (use-in-some-way foo)))


> Most of the time, lexical scope is likely to be more useful.  But
> there are places where dynamic scope is to be preferred.

I disagree.  The only time I have found a lexical scope not able do
the job is when the scope is too large to be convenient for a single
file, and rarely if some code needs access to a scope but can't be
physically placed within it.  But in those cases, a way for different
lexical scopes to share a namespace is enough.

Dynamic scoping should die fast.


-- 
CARL BANKS
http://www.aerojockey.com



More information about the Python-list mailing list