Favorite non-python language trick?

Shai shai at platonix.com
Thu Jul 7 08:20:15 EDT 2005


Mike Meyer wrote:
> "Shai" <shai at platonix.com> writes:
>
> > They're called "Special vars", and you need to define them (unlike
> > local LISP variables, which behave essentially like Python vars), but
> > then you use them just like other vars (that is, you usually bind them
> > with LET). This is the first I hear about them being ill-considered in
> > LISP; http://www.gigamonkeys.com/book/ is a recently published LISP
> > book which recommends them. I don't know about Scheme, but I think it
> > does have them.
>
> I'm pretty sure scheme doesn't have dynamically bound variables. I
> just went through r5rs to check, and couldn't find them.
>

Yes, you're right. One learns.

> > dynamic x=10
> > def bar():
> >   print x
> >
>
> This looks different from what I understood before. You're now
> declaring the variable dynamic in the global scope, rather than in the
> function that makes it dynamic. This is a *much* more palatable
> situation.
>

This is indeed different from what I said first. It copies the Common
LISP construct without regard to consistency with the Python global
construct.

> Globals are lexically scoped. As such, you can find the defintion of
> the variable by examining the module that includes the function. Yes,
> other modules can reach into your module and change them - but you can
> find those, because they reference your module by name.
>
> A dynamic variable declared so in a function has no such clue
> associated with it. If the variable is declared dynamic in the module
> of the enclosed function, that provides a contextual clue.

In my original proposal, dynamic variables are seen as globals from the
function in their module which reads them; no more, no less. The
important point I want from dynamic scope is the time-locality of
asignments, that
is, the fact that they are undone when the (lexical) scope of the new
binding ends. This allows the use of globals, with a lot less fear of
unintended interactions between users of the module (well, this is only
accurate until multithreading enters the picture, but that can be
handled
too).

[rest snipped]




More information about the Python-list mailing list