3 ms·
> Are there any good reasons why Python can't allow both? That is, you want [a for b in c for a in b] to represent both foreach(c, foreach(b)) and foreach(b, f
by llimllib 15y ago
> Are there any good reasons why Python can't allow both?
That is, you want [a for b in c for a in b] to represent both foreach(c, foreach(b)) and foreach(b, foreach(c))? Surely it must choose one or the other.
You can sensibly argue that the order should be reversed, but not that python should allow both. There should be only one obvious way to do things.
> Does this feel weird to anyone else?
Not to me; [(x,y) for x in a for y in b] translates simply to:
ret = []
for x in a:
for y in b:
ret.append((x,y))
- deleted 15y ago[deleted]
- cousin_it 15y agoYeah, you're right. Allowing both is a non-starter. Well, it could try to infer the direction by looking which way the bindings go, e.g. [a for b in c for a in b] would represent foreach(c, foreach(b)) because otherwise b would be unbound in the outer loop... but that doesn't sound like an especially good idea, having syntax change depending on which variable bindings exist. So all in all, I guess I'd prefer it to always go right-to-left: [a for a in b for b in c]. That would be consistent with nested comprehensions (which read right-to-left), but not with nested loops (which read top-to-bottom). YMMV.
- deleted 15y ago[deleted]