"no variable or argument declarations are necessary."

Paul Rubin http
Fri Oct 7 10:21:33 EDT 2005


Mike Meyer <mwm at mired.org> writes:
> > When you want local variable in lisp you do :
> >
> > (let ((a 3)) (+ a 1))
> 
> Excep that's not a decleration, that's a binding. That's identical to
> the Python fragment:
> 
>        a = 3
>        return a + 1
> 
> except for the creation of the new scope. Not a variable decleration
> in site.

Really not though, the scope is what makes it a declaration.  In
Python you can say:

   for i in 1,2:
     if i == 2: print x   # x is unbound the first time through the loop
     x = 9                # but bound the second time 

and the print doesn't raise an error.  Further, 'x' is still in scope
even after the loop exits.  Scheme lets you say something like

  (define (counter)
    (let ((k 0))
      (define (f)
         (set! k (+ 1 k))
         k)
      f))

Yeah, I know you'd write it a bit more concisely in Scheme, but I
wanted to make it look like the Python equivalent:

  def counter():
     k = 0
     def f():
        k += 1
        return k
     return f

The trouble is, in f, k is neither local nor global, so Python throws
up its hands and raises an error.  There's no way to tell it where to
find k.  This could be solved with a declaration, if Python understood it.




More information about the Python-list mailing list