3 ms·
No, it's an iterable containing named tuples, not iterating over a named tuple.
by wiredfool 9y ago
No, it's an iterable containing named tuples, not iterating over a named tuple.
- infogulch 9y agoI suppose if python had object destructuring like newish javascript (I think that's what it's called) it would be trivial. I don't know what you'd do instead in this case that doesn't involve adding additional methods or something.
- devnonymous 9y agoObject destructuring is tuple unpacking in python which is exactly what the code example does. What the parent was remarking on was caring about the performance of that rather than the ability.
- the_mitsuhiko 9y agoExcept in javascript you can unpack by name. This would solve this issue as well.
- filmor 9y agoThat's not the point, destructuring works fine on classes implementing `__iter__`: class A: def __iter__(self): return iter(range(3)) for x, y, z in [A(), A(), A()]: print(x, y, z) Prints 0 1 2 0 1 2 0 1 2
- carapace 9y agoTIL, cheers!
- scbrg 9y agoIt's both. The unpacking of the individual tuples are managed through the iterator protocol as well.