That can be solved by implementing __iter__ method on the class, isn't it?
Re:Redesigning Python's named tuples
by joshuamorton on Friday September 01, 2017 @11:31AM
(#15149287)
Sure, 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.
No, it's an iterable containing named tuples, not iterating over a named tuple.
That'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 2TIL, cheers!
It's both. The unpacking of the individual tuples are managed through the iterator protocol as well.
I 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.
Re:Redesigning Python's named tuples
by devnonymous on Friday September 01, 2017 @04:25AM
(#15146972)
Object 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.
Re:Redesigning Python's named tuples
by the_mitsuhiko on Friday September 01, 2017 @06:31AM
(#15147337)
Except in javascript you can unpack by name. This would solve this issue as well.
I do things like
all the time