A critic of Guido's blog on Python's lambda

Ken Tilton kentilton at gmail.com
Fri May 12 14:58:38 EDT 2006



Michele Simionato wrote:
> jayessay wrote:
> 
>>I was saying that you are mistaken in that pep-0343 could be used to
>>implement dynamically scoped variables.  That stands.
> 
> 
> Proof by counter example:
> 
> from __future__ import with_statement
> import threading
> 
> special = threading.local()
> 
> def getvar(name):
>     return getattr(special, name)
> 
> def setvar(name, value):
>     return setattr(special, name, value)
> 
> class dynamically_scoped(object):
>     def __init__(self, name, value):
>         self.name = name
>         self.value = value
>     def __context__(self):
>         return self
>     def __enter__(self):
>         self.orig_value = getvar(self.name)
>         setvar(self.name, self.value)
>     def __exit__(self, Exc, msg, tb):
>         setvar(self.name, self.orig_value)
> 
> if __name__ == '__main__': # test
>     setvar("*x*", 1)
>     print getvar("*x*") # => 1
>     with dynamically_scoped("*x*", 2):
>         print getvar("*x*") # => 2
>     print getvar("*x*") # => 1
> 
> If you are not happy with this implementation, please clarify.

Can you make it look a little more as if it were part of the language, 
or at least conceal the wiring better? I am especially bothered by the 
double-quotes and having to use setvar and getvar.

In Common Lisp we would have:

    (defvar *x*) ;; makes it special
    (setf *x* 1)
    (print *x*) ;;-> 1
    (let ((*x* 2))
       (print *x*)) ;; -> 2
    (print *x*) ;; -> 1

kenny

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.



More information about the Python-list mailing list