Skip to content
Advertisement

Tag: generator-expression

What type of iterable is “x for x in y”?

I’ve been confused of list comprehension in Python, although its shorthand doing for loop is very convenient. I’m not sure if what’s in the join() function’s parameter below is list comprehension since usually you put [] around to show it’s a list comprehension. My question: What’s the type of iterable produced by str(x) for x in res[::-1] in join() below?

Generator as function argument

Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules? If we have: This works, as expected. This does not work, as expected. This works, as expected This works, but I don’t understand why. Shouldn’t it fail in the same way as 2) Answer Both 3. and 4. should be

Advertisement