[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake

Jan Kanis jan.kanis at phil.uu.nl
Thu Oct 9 12:06:14 CEST 2008

On 08/10/2008, Bruce Leban <bruce at leapyear.org> wrote:
> I don't think this is better. Not that I'm proposing we add macros to the
> language but if we did then
>     macro find(a, x, i):
>         for i in range(len(x)):
>             if x[i]:
>                 return lambda: x[i]
> would operate differently for find(a, x, i) and find(a, x, i[0]). I think
> that's a bad idea.

No, it wouldn't. The loop stops iterating as soon as one lambda is
created, so the whole situation which started this thread, with
multiple lambdas being created in a loop and each next iteration
'overwriting' the variable the previous lambdas refer to, does not
occur. Your example would behave the same under current semantics,
under my proposal, and under my proposal with complex variables also
being 'celled' (the variant I don't prefer).

Also, as you say, Python doesn't actually have macros and isn't going
to get them any time soon. Saying that my proposal doesn't interact
nicely with feature X which python doesn't have and is not going to
get in the forseeable future is not really a convincing argument.

You are more likely to convince me if you can show an actual example
in which a[0] or an other non-simple variable is used as index
variable. Like I said before, I don't think such a situation occurs,
and if it does you'd want the behaviour I defended previously.


More information about the Python-ideas mailing list