4 ms·
That can be solved by implementing __iter__ method on the class, isn't it?
by Grue3 9y ago
That can be solved by implementing __iter__ method on the class, isn't it?
- wiredfool 9y agoNo, 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.
- joshuamorton 9y agoSure, but if you want an immutable object with iter, reasonable equality operators baked in, and named access, you can either define iter, eq, neq, and le on a new style slotted object with a bunch of getter descriptors, or you can make a namedtuple.