7 ms·
Are you joking or serious? I can't read your tone without emoji.
by xapata 9y ago
Are you joking or serious? I can't read your tone without emoji.
- emmelaich 9y agoSerious; see coldtea's reply for a fuller explanation. Or consider (firstname="John", lastname="Doe") vs (lastname="Doe", firstname="John"). I think you'd agree that they're the same.
- rraval 9y agoThey are the "same" by equality, but that's not the only relevant comparison. Ordering is important if you want lexicographic less than. Without ordering, you wouldn't be able to sanely run `sorted` on a iterable of namedtuples.
- tomrod 9y agoDumb question from a non-developer: wouldn't using a dict object make more sense if someone wanted an unordered named tuple?
- coldtea 9y agoIt's not about the storage (besides Python also has order dicts). Namedtuples are for the convenience of creating a record type (though indeed as others say, treating it as tuple where things expect a tuple is also a benefit).
- xapata 9y agoA dict is appropriate for labeled data. Currently unordered, but likely to keep insertion order in v3.7 and later. Don't call it an unordered namedtuple. That's confusing, because tuples are ordered.
- emmelaich 9y agoYou could order with a custom comparison; specify the columns* to be ordered as in a SQL 'ORDER BY'.
- cocoablazing 9y agoI would expect that construction to result in identical behavior for dict() and certainly not anything calling itself a tuple. NamedTuples behaving in the manner you expect violates LSP with respect to tuple().
- xapata 9y agoI disagree. As of v3.6, the order of keyword arguments is preserved.