Favorite non-python language trick?

Mike Meyer mwm at mired.org
Fri Jul 1 18:25:21 EDT 2005


"Shai" <shai at platonix.com> writes:

> Joseph Garvin wrote:
>>
>> I'm curious -- what is everyone's favorite trick from a non-python
>> language? And -- why isn't it in Python?
>
> 1. Lisp's "dynamically scoped" variables (Perl has them, and calls them
> "local", but as far as I've seen their use their is discouraged). These
> are global variables which are given time-local bindings. That is,
> structuring the syntax after what's used for globals,

Perl started life with nothing but dynamically scoped variables. They
added lexical scoping after they realized what a crock dynamic scoping
was.

> x=10
> def foo():
>   # No need to define x as it is only read -- same as globals
>   print x
>
> def bar():
>   dynamic x
>   x = 11
>   foo()
>
> def baz():
>   bar() # prints 11
>   foo() # prints 10; the binding in bar is undone when bar exits
>
> This feature makes using "globals" sensible, providing a way to avoid
> many important uses (and some say, misuses) of objects if you are so
> inclined. It allows you to do some things better than objects do,
> because it does to library parameters, what exceptions do to return
> codes: instead of passing them in all the way from outside until a
> piece of code which actually uses them, they are only mentioned where
> you set them and where you really need to access them.
>
> It would not be too hard to implement a version of this (inefficiently)
> in the existing language, if frame objects could carry a modifiable
> dictionary.
>
> I suppose it is not in Python because (most) Pythoners are not looking
> (hard enough) for alternatives to OOP.

Last time I checked, dynamic binding variables were frowned on in LISP
systems as well. Scheme doesn't have them.  Common LISP requires
special forms to use them.

The problem with the given use case is that it lets every routine in
the call chain substitute it's own variable for the library parameter
you want to use, with no local indication that this is going
on. This makes bugs in dynamically scoped variables a PITA to find.

Given that it's a feature I don't want programmers using, I'd only be
willing to see it added to the language if you can show that it has no
overhead so long as you don't use it. I'm not sure that can be done.

         <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list