Fun with misleading errors and generator expressions! It took me a couple hours to figure out an error I was getting; here’s a boiled-down version.
First, here’s what’s supposed to happen:
>>> def joiner(tuples): ... generator = (str(x[0]) for x in tuples) ... return ' '.join(generator) ... >>> joiner(((1, 2, 3), (4, 5, 6))) '1 4' Now suppose something's gone wrong with the input and it isn't nested: >>> joiner((1, 2, 3)) Traceback (most recent call last): File "", line 1, in ? File "", line 3, in joiner TypeError: sequence expected, generator found Say what? I'd expect that to give "TypeError: unsubscriptable object" because the first time through the generator expression, you're effectively doing this: >>> 1[0] Traceback (most recent call last): File "", line 1, in ? TypeError: unsubscriptable object
So that’s your real problem, but somewhere else (in str.join i guess?) that gets caught and turned into “sequence expected, generator found”. Ugh. Which is really misleading; you *can* call str.join on a generator as long as the generator returns strings and doesn’t blow up.
This took so long to diagnose because all the code (including the generator) is third-party, and it *usually* worked, and the problem turned out to be triggered by me passing a bad value for a seemingly unrelated string parameter (I failed to capitalize it). And pdb isn’t very useful with generator expressions (I have no idea why the guy was even using one there). And you can’t step into str.join() either since it’s a C function.

No Comments
RSSNo comments yet.
Leave a comment