default value in __init__
Lawrence D'Oliveiro
ldo at geek-central.gen.new_zealand
Fri Oct 17 06:04:52 EDT 2008
In message <Xns9B33BC4CC1480duncanbooth at 127.0.0.1>, Duncan Booth wrote:
> We already get people asking why code like this doesn't return 3:
>
>>>> fns = [ lambda: x for x in range(10) ]
>>>> fns[3]()
> 9
>
> ... making this change to default arguments would mean the
> solution usually proposed to the function scoping question above would no
> longer work:
>
>>>> fns = [ lambda y=x: y for x in range(10) ]
>>>> fns[3]()
> 3
The right solution, of course, is
fns = [(lambda x : lambda : x)(x) for x in range(10)]
More information about the Python-list
mailing list