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

michele.simionato at gmail.com michele.simionato at gmail.com
Tue May 9 09:37:21 CEST 2006


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




More information about the Python-list mailing list