Dynamic scoping

Alex Martelli aleaxit at yahoo.com
Wed Jun 6 05:57:44 EDT 2001


"Arun Sharma" <adsharma at sharmas.dhs.org> wrote in message
news:slrn9hr0ft.1kj.adsharma at eagle.mirabella.net...
> I've searched the archives of this group as well as the net, about the
> advantages and disadvantages of lexical vs dynamic scoping. I'm
> convinced by the arguments that lexical scoping is what people want
> most of the time.
>
> However, under some circumstances, people might want to use dynamic
> scoping. Is there a reason why python doesn't support it ? (Unlike
> perl ?).

But it does, if you want it badly enough -- why do you think
the exec statement and eval function let you pass dictionaries
to specify the global and local namespaces to use?  Why, it's
*exactly* (among other things:-) so you can dynamically build
whatever scope you need... the amount of trouble you have to
go to in order to obtain the right codeobject for eval or exec
is accurately proportioned to correspond to just the right
degree of dissuasion of use of dynamic scoping (1:99.44 being
the mathematically exact and indisputable ratio thereof...:-).

Seriously: for that 1 time in 100 you do want dynamic scoping,
and are exactly right in thus wanting, eval/exec/execfile with
specified dictionaries are just right.  AND they give you far
more control than bare dynamic-scoping would... plus, their
relative obscurity helps ensure that somebody using them has
that solid grasp of Python needed to ensure they REALLY do
want to scope dynamically.  Probably a serendipitous result
rather than a fully-though-out design decision, but who
can tell -- Guido moves in mysterious ways, his wonders
to perform, ya know...


Alex






More information about the Python-list mailing list