4 ms·
Proposed style guide: When you want to get the (n+1)th item from a list, do: item = stuff[n] To get the first item, do: item = stuff[0] Unless the list has
by torrentabuse 16y ago
Proposed style guide:
When you want to get the (n+1)th item from a list, do:
item = stuff[n]
To get the first item, do:
item = stuff[0]
Unless the list has one element, then do:
(item, ) = stuff
At least you'll never be accused of consistency.
- StavrosK 16y agoNot really. The first two are consistent, but the last one is "get the only item and assert that there is only one item to get. If you don't care about the assertion, use [0], as always. I like the latter, because there's added info for the reader ("this should return a one-item iterable"). It's not really inconsistent, because you aren't performing the same operation in the two cases.
- spoondan 16y agoI'm annoyed by the belief that consistency is ipso facto good. Consistency is a tool for attacking a particular type of problem. Consistency for its own sake will often make things worse. I see this fallacious reasoning all the time in design critiques: this is inconsistent with that. Well, yes, but so what? Why is consistency desirable in this instance?