4 ms·
I'm a bit surprised nobody has tried to start up the "what are tuples really for in Python" argument in this thread. The definition given -- "Python tuples act
by ubernostrum 9y ago
I'm a bit surprised nobody has tried to start up the "what are tuples really for in Python" argument in this thread.
The definition given -- "Python tuples act like immutable lists" -- is correct as a description of behavior, but the decision of when and whether to use a tuple versus a list or some other structure can be a pretty contentious one in some Python circles.
- cup-of-tea 9y agoLists are for iterating, tuples are for unpacking.
- kzrdude 9y agoWouldn't you say that lists are for homogenous collections and tuples for short heterogeneous ones? Of course we use the Python "duck typing" definition for homogenous.
- cup-of-tea 9y agoYou could, but I think mine is better. (firstname, lastname) and (x,y,z) are homogeneous collections. But they probably should be tuples. In a tuple the position describes the structure, in a list the position merely describes the order. Hence why tuples are best if you plan to unpack them (ie. firstname, lastname = name). Trouble is Python doesn't completely separate the concepts like some languages do (e.g. C array/struct, R array/list). You can unpack lists and store heterogeneous items in them. Many libraries do this (e.g. SQL libraries will give a list per row by default). And if you follow the bash read idiom in Python using str.split you'll unpack a list as well. For me the distinction is about documenting my code. The only real practical advantage I'm aware of is tuples are hashable. I don't think they are implemented anything like structs underneath.
- lmm 9y ago> (firstname, lastname) and (x,y,z) are homogeneous collections. Disagree. In a language with a decent type system you would give them different types, because using a first name as a last name or an x as a z is always an error.
- shoo 9y agoYeah. Another rule of thumb is that a lot of the time you can make code that uses tuples clearer by rewriting it to use namedtuple-defined struct values. If the language supported it, it'd probably also make sense most of the time to define a type for each tuple field, and have a type-checker look for errors. Occasionally you see tuples used where they want immutable and hashable lists, but most of the time they're used as shorthand struct/"pod" tours types.
- cup-of-tea 9y agoIn what language would you make firstname and lastname or x and y different types?
- lmm 9y agoWell, I would in Rust for starters.
- cup-of-tea 9y agoInteresting. I should probably try one of these typed languages. When I was thinking of types I was thinking about "physical" types like in C, not abstract types.
- dllthomas 9y agoYeah, it's pretty common to think of types as "representational", and that really doesn't get at most of what they are good for. Even in C you can get some good help from the type checker if you know how to use it. For instance, see https://dlthomas.github.io/using-c-types-talk/slides/slides.html#26.0 https://dlthomas.github.io/using-c-types-talk/slides/slides.... for a technique that can turn a flakey segfault from misuse of an API into a compile time error (later slides do just that for a simple SDL program, with a modified header file).