4 ms·
In most languages you can't usually: 1. Iterate over a tuple 2. Convert a list to a tuple 3. Construct a tuple of a length not known at compile-time Python
by alextgordon 12y ago
In most languages you can't usually:
1. Iterate over a tuple
2. Convert a list to a tuple
3. Construct a tuple of a length not known at compile-time
Python allows these because "why not?" but it does break their "one and only one way to do it" rule and confuses beginners a hell of a lot.
There are definitely borderline cases. For instance, should a Vector be a list or a tuple? A Vec3 type is obviously a tuple, but a large Vector destined for BLAS is obviously a list.
- dragonwriter 12y ago> Python allows these because "why not?" No, it allows them because the distinction that those restrictions are founded on is only useful in a statically-typed languages, and Python isn't statically typed. > For instance, should a Vector be a list or a tuple? A real vector/array should be its own data type (probably implemented in a C, or similar low-level, extension) that happens to implement the interface expected of an indexable, iterable collection, neither a list nor a tuple.
- jl6 12y agoSo why does Python distinguish between a list and a tuple at all?
- hueving 12y agoOne is mutable, one is not. Performance. Also, look up namedtuple. Useful for returning multiple values.
- dragonwriter 12y agoBecause the distinction between a mutable list and an immutable list is still meaningful in a dynamic language like python.
- dozzie 12y ago> [this] distinction [...] is only useful in a statically-typed languages ...like Erlang.
- dragonwriter 12y agoYeah, while snarkily made its a good point that Erlang does make use of it without being statically typed. There is a deep difference that goes beyond use of tuples in language approach between Python and Erlang here where it comes to types in which Erlang, while dynamically typed, has a deep concern for types in its pattern matching system to make path decisions while Python is very much centered on using dynamic OO techniques -- how objects respond to messages -- to do that. So I'd still say its the same kind of deep language approach difference at work.