Re: Python Mystery Theatre -- Episode 2: Así Fue
Helmut Jarausch
jarausch at skynet.be
Mon Jul 14 16:13:08 EDT 2003
Raymond Hettinger wrote:
> ACT III ----------------------------------------------------
> def once(x): return x
> def twice(x): return 2*x
> def thrice(x): return 3*x
> funcs = [once, twice, thrice]
>
> flim = [lambda x:funcs[0](x), lambda x:funcs[1](x), lambda x:funcs[2](x)]
> flam = [lambda x:f(x) for f in funcs]
>
> print flim[0](1), flim[1](1), flim[2](1)
> print flam[0](1), flam[1](1), flam[2](1)
OK, I believe to know why the last line
print '3' three times, since only a reference
to 'f' is stored within the lambda expression
and this has the value 'thrice' when 'print'
is executed.
But how can I achieve something like an
evaluation of one indirection so that
a reference to the function referenced by 'f'
is stored instead.
Thanks for hint.
(this references only model of Python is
a bit hard sometimes)
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
More information about the Python-list
mailing list