7 ms·
It's called a tuple so why would accessing its elements by index be "bizarre" in any way?
by adambyrtek 9y ago
It's called a tuple so why would accessing its elements by index be "bizarre" in any way?
- emmelaich 9y agoBecause order should be unimportant for a tuple.
- xapata 9y agoAre 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.
- lou1306 9y agoTypically tuples are ordered lists of values. See Codd's relational model, Linda, Erlang, etc.
- coldtea 9y agoIn the relational model the order shouldn't matter: A relation is defined as a set of n-tuples. In both mathematics and the relational database model, a set is an unordered collection of unique, non- duplicated items, although some DBMSs impose an order to their data. In mathematics, a tuple has an order, and allows for duplication. E. F. Codd originally defined tuples using this mathematical definition. Later, it was one of E. F. Codd's great insights that using attribute names instead of an ordering would be so much more convenient (in general) in a computer language based on relations. This insight is still being used today. Though the concept has changed, the name "tuple" has not.
- lou1306 9y agoI stand corrected; I was probably fooled by the naming. Still, in most contexts tuples are ordered while sets/bags are not.
- deleted 9y ago[deleted]
- adambyrtek 9y agoOrder is important for "pure" Python tuples, so I see no reason why it shouldn't be the case for namedtuple. Mathematical tuples are ordered as well: "In mathematics a tuple is a finite ordered list (sequence) of elements. An n-tuple is a sequence (or ordered list) of n elements, where n is a non-negative integer. There is only one 0-tuple, an empty sequence. An n-tuple is defined inductively using the construction of an ordered pair." (Wikipedia)
- ageofwant 9y agoTuples are ordered by definition. We use names for things to convey what they are, not what we want them to be. If you want a construct in which order does not matter, call it a set or a dict.
- joshuamorton 9y agoFollowup: Should order be important for a Tuple under a system that defines a Tuple as an immutable ordering? That is, whatever your philosophical issues with namedtuple ordering, in python, a tuple is an immutable ordered list of things. In practice, tuples have taken the role of being type-heterogenous, known length, records. One might then reasonably expect a namedtuple to be a type-heterogneous, known length, record, where attributes are named.
- emmelaich 9y agoFWIW I was referring to the abstract pure tuple, not the tuple as implemented in Python and some other languages where order is very useful. "inessential" might be a better than "unimportant"
- coldtea 9y agoBecause the whole point of namedtuples is to have them as a convenient quick notation for records. But I was referring to the parent's general observation that about "catering to some obscure use case that nobody uses", which I agree with -- not about their specific example.