[Python-Dev] 2.5 and beyond

Scott Dial scott+python-dev at scottdial.com
Sat Jul 1 01:13:58 CEST 2006


Guido van Rossum wrote:
> On 6/30/06, Ka-Ping Yee <python-dev at zesty.ca> wrote:
>> On Fri, 30 Jun 2006, Andrew Koenig wrote:
>>> I hope Py3000 has lexical scoping a la Scheme...
>> Me too -- that would be really nice.
> 
> That's not a very constructive proposal (especially since I don't know
> Scheme). Perhaps you could elaborate on what needs to change?

I believe the essence of their request for lexical scope boils down to 
allowing rebinding. Such code like the following is legal in Scheme:

def f(x):
     def incr():
         x = x + 1
         return x
     def decr():
         x = x - 1
         return x
     return (incr, decr)

(incr, decr) = f(1)
print incr() # 2
print incr() # 3
print decr() # 2
print decr() # 1

--
FWIW, the Scheme equivalent would be something like:

(define f (lambda (x)
   (list
     (lambda () (set! x (+ x 1)) x)
     (lambda () (set! x (- x 1)) x))))

(let ([fs (f 1)])
   (let ([incr (car fs)] [decr (cadr fs)])
     (display (incr)) (newline)    ; 2
     (display (incr)) (newline)    ; 3
     (display (decr)) (newline)    ; 2
     (display (decr)) (newline)))  ; 1


As a more personal aside, I can't imagine where I would use this in any 
python program I have ever wrote. I actually never noticed that 
rebinding wasn't allowed until recently.

-- 
Scott Dial
scott at scottdial.com
scodial at indiana.edu


More information about the Python-Dev mailing list