News for hackers. Stuff that matters.
Re:Redesigning Python's named tuples by Grue3 on Friday September 01, 2017 @03:05AM (#15146704)
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.
Re:Redesigning Python's named tuples by wiredfool on Friday September 01, 2017 @03:21AM (#15146752)
No, it's an iterable containing named tuples, not iterating over a named tuple.
Re:Redesigning Python's named tuples by filmor on Friday September 01, 2017 @04:14AM (#15146939)
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 2
Re:Redesigning Python's named tuples by carapace on Friday September 01, 2017 @04:55PM (#15152333)
TIL, cheers!
Re:Redesigning Python's named tuples by scbrg on Friday September 01, 2017 @04:24AM (#15146970)
It's both. The unpacking of the individual tuples are managed through the iterator protocol as well.
Re:Redesigning Python's named tuples by infogulch on Friday September 01, 2017 @04:01AM (#15146900)
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.