How does this function work?

exarkun at divmod.com exarkun at divmod.com
Mon Sep 20 17:35:10 EDT 2004


On Mon, 20 Sep 2004 13:05:27 -0700, "Robert Brewer" <fumanchu at amor.org> wrote:
>Jp wrote:
> >   >>> def f(y):
> >   ...     return [x for x in range(3), 1, y for y in range(4), 4]
> >   ...
> >   >>> f(3)
> >   [[0, 1, 2], [0, 1, 2], 1, 1, 3, 3]
> >   >>>
> >
> >   I've been staring at it for 15 minutes and I'm no closer to=20
> > understanding than when I started.
> >
> >   Why is this even legal Python syntax?  What is going on=20
> > that makes it return what it does?
> 
> A couple of parentheses might get you started. Your function is exactly
> the same if you write:
> 
> >>> def g(y):
> ...     return [x for x in (range(3), 1, y) for y in (range(4), 4)]
> 

  That helps, indeed.  Thanks :)

  Jp



More information about the Python-list mailing list