functional programming and default parameters
Gareth McCaughan
Gareth.McCaughan at pobox.com
Sat Jun 30 16:11:25 EDT 2001
Tim Peters wrote:
> [Ralf Muschall]
> > ...
> > Unfortunately, in Python writing
> >
> > for i in range(3):
> > j=i
> > fns[i]()=lambda:j
> >
> > fails, and even this fails too:
> >
> > for i in range(3):
> > j=i+10
> > a[i]=lambda:j
> >
> > map(x:x(),fns) gives [12,12,12].
...
> No, Python does no optimizations at all: WYSIWYG. But you're carrying a
> scope model over from Lisp that doesn't apply in Python. In general, the
> only way to get a Lispish new scope in Python is via nesting a def.
I'm not sure Lisp experience is the culprit here.
(mapcar #'funcall (loop for i upfrom 1 to 5 collecting (lambda () i)))
==> (6 6 6 6 6)
An equivalent with DOTIMES is just the same, by the way.
I don't remember reading Ralf's original article, so if that
proves that his expectation *is* derived from Lisp then what
I'm not sure of is that Ralf has correctly understood Lisp. :-)
--
Gareth McCaughan Gareth.McCaughan at pobox.com
.sig under construc
More information about the Python-list
mailing list