There's a reason that construct is often referred to as tuple unpacking :)
In that model, the tuple is just a convenient way to pass a collection of values around, which you then unpack into individual elements when you want to make use of it.
Where namedtuples come in is that sometimes you're only interested in one or two elements from the tuple - with a namedtuple, it becomes more practical to reference those *without* needing to unpack it.
Sure, I wasn't suggesting that classic tuples ...
Steve · 2013-04-28T07:52:03.804000
Sure, I wasn't suggesting that classic tuples should be discarded - simply that I doubt named tuples are indexed very much.
I get that much.
But now that the conversation ...
toddrme2178 · 2013-04-28T09:08:10.766000
I get that much.
But now that the conversation has veered off into named tuples, what I don't really understand is the advantage of named tuples over dicts (or at least orderedicts).
To access element joe from a dict you must write
...
Steve · 2013-04-28T09:50:23.054000
To access element joe from a dict you must write
x = d["joe"]
whereas to access the same element in a named tuple you would write'
v = d.joe
When the names are known this is a much simpler way to write the equivalent acces. And, as Nick pointed out, you can't use unpacking assignments with a dict.
Yes, the syntax is a little shorter. On the other...
toddrme2178 · 2013-04-28T10:14:07.014000
Yes, the syntax is a little shorter. On the other hand it muddles the distinction between keys and class attributes.
And you can do unpacking assignment with an orderdict using .values().
Perhaps you can, but you cannot predict the order ...
Steve · 2013-04-28T10:18:08.416000
Perhaps you can, but you cannot predict the order in which the values will be produced, so it isn't very useful for an unpacking assignment, is it?
You can for ordereddict.
toddrme2178 · 2013-04-28T13:06:15.931000
You can for ordereddict.
"...perhaps it's time to either remove na...
claudio canepa · 2013-04-28T14:31:27.140000
"...perhaps it's time to either remove namedtuple's __getitem__() and similar methods or implement a similar object without the sequence behaviors"
What if I want to use some generic code to "trying to save its state during a pickling operation or some more obscure book-keeping metacode"
tuples are cheaper in terms of memory, that's ...
Samus_ · 2013-04-28T14:32:09.623000
tuples are cheaper in terms of memory, that's the only reason they exist.
anything that can be done with a tuple can (and most of the time is) done with a list.
in Haskell a 2-tuple is different from a 3-tuple so it make more sense in the context exposed on this article but Python won't bother, it'll fail with the wrong sized tuple the same way it would with a wrong sized list.
tuples are cheaper in terms of memory, that's ...
Samus_ · 2013-04-28T14:37:30.908000
tuples are cheaper in terms of memory, that's the only reason they exist.
everything that can be done with a tuple can (and usually is) done with a list.
in Haskell a 2-tuple is different than a 3-tuple and (str, int) is different from (int, str) but Python won't bother, the wrong tuple will fail the same as the wrong list.
the only thing tuples can do that lists not is being hashed but I doubt a tuple was used as a key to anything ever.
Ah, but you miss the other way to use classic tupl...
Anonymous · 2013-04-28T04:52:04.720000
Ah, but you miss the other way to use classic tuples:states, symbols, transition, initial, accepting_states = nfa
There's a reason that construct is often referred to as tuple unpacking :)
In that model, the tuple is just a convenient way to pass a collection of values around, which you then unpack into individual elements when you want to make use of it.
Where namedtuples come in is that sometimes you're only interested in one or two elements from the tuple - with a namedtuple, it becomes more practical to reference those *without* needing to unpack it.
Sure, I wasn't suggesting that classic tuples ...
Steve · 2013-04-28T07:52:03.804000
Sure, I wasn't suggesting that classic tuples should be discarded - simply that I doubt named tuples are indexed very much.I get that much. But now that the conversation ...
toddrme2178 · 2013-04-28T09:08:10.766000
I get that much.But now that the conversation has veered off into named tuples, what I don't really understand is the advantage of named tuples over dicts (or at least orderedicts).
To access element joe from a dict you must write ...
Steve · 2013-04-28T09:50:23.054000
To access element joe from a dict you must writex = d["joe"]
whereas to access the same element in a named tuple you would write'
v = d.joe
When the names are known this is a much simpler way to write the equivalent acces. And, as Nick pointed out, you can't use unpacking assignments with a dict.
Yes, the syntax is a little shorter. On the other...
toddrme2178 · 2013-04-28T10:14:07.014000
Yes, the syntax is a little shorter. On the other hand it muddles the distinction between keys and class attributes.And you can do unpacking assignment with an orderdict using .values().
Perhaps you can, but you cannot predict the order ...
Steve · 2013-04-28T10:18:08.416000
Perhaps you can, but you cannot predict the order in which the values will be produced, so it isn't very useful for an unpacking assignment, is it?You can for ordereddict.
toddrme2178 · 2013-04-28T13:06:15.931000
You can for ordereddict."...perhaps it's time to either remove na...
claudio canepa · 2013-04-28T14:31:27.140000
"...perhaps it's time to either remove namedtuple's __getitem__() and similar methods or implement a similar object without the sequence behaviors"What if I want to use some generic code to "trying to save its state during a pickling operation or some more obscure book-keeping metacode"
tuples are cheaper in terms of memory, that's ...
Samus_ · 2013-04-28T14:32:09.623000
tuples are cheaper in terms of memory, that's the only reason they exist.anything that can be done with a tuple can (and most of the time is) done with a list.
in Haskell a 2-tuple is different from a 3-tuple so it make more sense in the context exposed on this article but Python won't bother, it'll fail with the wrong sized tuple the same way it would with a wrong sized list.
tuples are cheaper in terms of memory, that's ...
Samus_ · 2013-04-28T14:37:30.908000
tuples are cheaper in terms of memory, that's the only reason they exist.everything that can be done with a tuple can (and usually is) done with a list.
in Haskell a 2-tuple is different than a 3-tuple and (str, int) is different from (int, str) but Python won't bother, the wrong tuple will fail the same as the wrong list.
the only thing tuples can do that lists not is being hashed but I doubt a tuple was used as a key to anything ever.
Page 1 of 3 · Newer comments
Back to post