3 ms·
> Tuples shouldn't be differentiated from lists primarily in terms of mutable vs immutable. This. In particular, tuples are for collections whose length is fix
by Myrmornis 13y ago
> Tuples shouldn't be differentiated from lists primarily in terms of mutable vs immutable.
This. In particular, tuples are for collections whose length is fixed and meaningful, and for which you could give names to each field. Lists are for collections whose length doesn't correspond to anything in particular and which you could imagine growing and shrinking.
As an aside, django makes this mistake and confuses people: using tuples where semantically they should be lists, presumably out of a desire for immutability.
- dalke 13y agoA class's __bases__ is a tuple of base classes. The length is not fixed, and there's no special name for [i] other than (i+1)-th; only its ordering is important. Do believe that the use of a tuple here is a bug, like you do for Django? The __bases__ can be re-assigned. Assignment to an instance is guaranteed atomic in Python. Some of the list operations are not. Thread-safe guarantees might be another reason for restricting list's mutability.
- wahnfrieden 13y agoYou're right, at a certain point functionality and other characteristics make all the difference. It's just a generality for higher-level code I suppose.