[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Dynamical scoping

George Sakkis

1/14/2008 8:18:00 PM

What's the best way to simulate dynamically scoped variables ala
Lisp ? The use case is an open-ended set of objects that need to
access the same piece of information (e.g. a dict, a ConfigParser
object, a logger etc.). I know that the "proper" OO and functional way
is to pass the information explicitly but that's less maintenable in
the long run. Also this is in a web environment so the information
can't be really global (though within-thread global should be fine).
Is there some standard pattern for this scenario ?

George
4 Answers

Paul Rubin

1/15/2008 1:14:00 AM

0

George Sakkis <george.sakkis@gmail.com> writes:
> What's the best way to simulate dynamically scoped variables ala Lisp ?

Ugh.... check the docs for the python 2.5 "with" statement, which
gives you sort of a programmable unwind-protect (more powerful than
try/except). You'd have an environment dictionary and use the "with"
statement to maintain a stack of shallow-binding cells like in an
old-time lisp system, automatically unwinding when the "with" suite
finishes. The whole concept sounds hopelessly crufty--I think nobody
even does it that way in Lisp any more, you're better off passing an
environment around explicitly. If there were a lot of variables, this
could be a good application for functional maps, which I've been wanting
to implemetn for python.

Kay Schluehr

1/15/2008 9:47:00 AM

0

On 15 Jan., 02:13, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> George Sakkis <george.sak...@gmail.com> writes:
> > What's the best way to simulate dynamically scoped variables ala Lisp ?
>
> Ugh.... check the docs for the python 2.5 "with" statement, which
> gives you sort of a programmable unwind-protect (more powerful than
> try/except). You'd have an environment dictionary and use the "with"
> statement to maintain a stack of shallow-binding cells like in an
> old-time lisp system, automatically unwinding when the "with" suite
> finishes. The whole concept sounds hopelessly crufty--I think nobody
> even does it that way in Lisp any more, you're better off passing an
> environment around explicitly. If there were a lot of variables, this
> could be a good application for functional maps, which I've been wanting
> to implemetn for python.

Kay Schluehr

1/15/2008 10:01:00 AM

0

On 14 Jan., 21:17, George Sakkis <george.sak...@gmail.com> wrote:
> What's the best way to simulate dynamically scoped variables ala
> Lisp ? The use case is an open-ended set of objects that need to
> access the same piece of information (e.g. a dict, a ConfigParser
> object, a logger etc.). I know that the "proper" OO and functional way
> is to pass the information explicitly but that's less maintenable in
> the long run. Also this is in a web environment so the information
> can't be really global (though within-thread global should be fine).
> Is there some standard pattern for this scenario ?
>
> George

What do you mean by "really global" and why is module local or builtin
a problem
in the presence of the GIL? Passing objects as parameters into
functions doesn't
ensure any more thread safety. Where do you think does Lisp ( which
one? ) stores
dynamically scoped variables?

Paul Rubin

1/15/2008 6:14:00 PM

0

Kay Schluehr <kay.schluehr@gmx.net> writes:
> On 15 Jan., 02:13, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> > George Sakkis <george.sak...@gmail.com> writes:
> > > What's the best way to simulate dynamically scoped variables ala Lisp ?
> >
> > Ugh.... check the docs for the python 2.5 "with" statement, which
> > gives you sort of a programmable unwind-protect (more powerful than
> > try/except). You'd have an environment dictionary and use the "with"
> > statement to maintain a stack of shallow-binding cells like in an
> > old-time lisp system, automatically unwinding when the "with" suite
> > finishes. The whole concept sounds hopelessly crufty--I think nobody
> > even does it that way in Lisp any more, you're better off passing an
> > environment around explicitly. If there were a lot of variables, this
> > could be a good application for functional maps, which I've been wanting
> > to implemetn for python.

Kay, did you have something to add? You quoted the above two posts
and then your message stopped.