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

Ken Tilton kentilton at gmail.com
Tue May 9 12:45:03 EDT 2006



michele.simionato at gmail.com wrote:
> Ken Tilton wrote:
> 
>>Python has a weak lambda, statements do not always
>>return values, it does not have macros, and I do not know if it has
>>special variables.
> 
> 
> I am pretty much ignorant of Common Lisp, but I have the impression
> they are the
> same as Scheme parameters, i.e. thread-local dynamically scoped
> variables
> (feel free to correct me if I am mistaken). If I am right, here is how
> you would emulate them in recent versions of Python:
> 
> import threading, time
> 
> special = threading.local()
> special.x = 0
> 
> def getx():
>     return special.x
> 
> def set_x(value):
>     special.x = value
>     time.sleep(3-value) # thread-2 completes after thread-1
>     print "%s is setting x to %s" % (threading.currentThread(), getx())
> 
> if __name__ == '__main__':
>     print getx() # => 0
>     threading.Thread(None, lambda : set_x(1)).start() # => 1
>     threading.Thread(None, lambda : set_x(2)).start() # => 2
>     time.sleep(3)
>     print getx() # => 0
> 
>            Michele Simionato
> 

I was not thinking about the thread issue (of which I know little). The 
big deal for Cells is the dynamic bit:

    (let ((*dependent* me))
         (funcall (rule me) me))

Then if a rule forces another cell to recalculate itself, *dependent* 
gets rebound and (the fun part) reverts back to the original dependent 
as soon as the scope of the let is exited.

In this case, because my code is so brilliant <g>, there is only one 
place in the code where a rule gets called, so it would be easy enough 
to manage by saving/restoring the existing value around storing a new 
value there.

I do wonder what would happen to Cells if I ever want to support 
multiple threads. Or in a parallel processing environment.

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