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

Jan Kanis jan.kanis at phil.uu.nl
Fri Oct 10 12:14:10 CEST 2008


On 10/10/2008, Bruce Leban <bruce at leapyear.org> wrote:
>
>
> On Thu, Oct 9, 2008 at 3:06 AM, Jan Kanis <jan.kanis at phil.uu.nl> wrote:
> >
> > 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.
> >
> OK, I screwed up that example.
>
> macro find(a, x, i):
>     for i in range(len(x)):
>        if x[i]:
>            z.append(lambda: x[i])

Yes, that one would behave differently... so IF macros were added
(which they won't), and IF someone would find it usefull to pass in
the index variable through the macro, instead of just using a regular
local variable and not exposing it (for which I don't see any use),
this macro would have to be SLIGHTLY REWRITTEN. (And as there are no
macros yet, there's no backward compatibility problem).

New version:

macro find(a, x, i):
    for j in range(len(x)):
        i = j        # but why would you want that?
        if x[j]:
            z.append(lambda: x[j])

Jan



More information about the Python-ideas mailing list