Feature request: subclassing FunctionType [Was: Some language proposals]

Michele Simionato michele.simionato at poste.it
Sat Feb 28 02:27:33 EST 2004


Jacek Generowicz <jacek.generowicz at cern.ch> wrote in message news:<tyffzcwbm7x.fsf at pcepsft001.cern.ch>....
> 
> I don't think that Python's behaviour differs from other languages, in
> this respect. For example in Common Lisp:
> 
> * (defun make-adders (n)
>     (loop for i below n collect
>   	  (lambda (x) (+ x i))))
> MAKE-ADDERS
> * (destructuring-bind (add0 add1) (make-adders 2)
>     (list (funcall add0 0) 
>   	  (funcall add1 0)))
> (2 2)

I don't know Common Lisp, but Scheme scope rules do what I expect:

(define (make-adders n)
  (unfold (cut = <> n) (lambda (i) (lambda (x) (+ x i))) add1 0))

(define-values (add0 add1) (apply values (make-adders 2)))

(print (add0 0)) ;=> 0
(print (add1 0)) ;=> 1

My feeling about Python scope rules are mixed: in the beginning, I thought
they were just broken, since they were not following my expectations
(I don't know where those expectations were coming from, maybe from
Pascal? I didn't know any functional language at that time, mah!).
Later on, when I learned about the default argument tricks, I thought 
there was a logic in there, i.e. I may choose if I want to use the value
of the "i" argument at the definition time or at the usage time
explicitly and "explicit is better than implicit". However, now that
I see that there are languages that implement the scope rule I
would expect, and since I don't see a real life case where Python 
default of using the value of "i" as the usage time is useful, I am 
starting questioning them again.
The only reason I see for the present state of the scope rules, is
to discourage people from using closures and using objects instead.
Which is not necessarely a bad thing, yes, but then I would need a better
support from the language, and the ability to subclass FunctionType
to be happy again ;)

         Michele Simionato



More information about the Python-list mailing list