3 ms·
Example of a typical list: [1,2,3,4,5] Example of some typical tuples: (1,'A') (2,'B') (3,'C') In Python, of course lists can be heterogenous, so you could ha
by kyllo 13y ago
Example of a typical list:
[1,2,3,4,5]
Example of some typical tuples:
(1,'A')
(2,'B')
(3,'C')
In Python, of course lists can be heterogenous, so you could have a list like [1, 'A', 2, 'B', 3, 'C'] but it would be an atypical/un-idiomatic use case. A list of tuples or possibly a dict would make more sense for that.
- dragonwriter 13y agoIn a dynamically typed language with a type heirarchy with a common root, it is impossible to distinguish between a heterogenous list whose entities are instances of a variety of specific types and a homogenous list whose members are all members of one of the common supertypes of the specific types of the objects. With a language that uses duck typing (so that the meaningful "types" of objects are often not their explicit classes but the implicit -- both in definition and in association with particular objects -- interfaces expected by the code which accesses them), this ambiguity is even more pronounced.
- kyllo 13y agoThis is true, and I'm sure Guido is fully aware of it, but it's also not entirely relevant to what he was saying. Regardless of inheritance, interfaces, and duck typing, the explicit classes of the objects still do matter. Yes, the list of [1,'A',2,'B',3,'C'] is really after all just a list of [Object, Object, Object, Object, Object, Object]. And the list itself is also just an Object. But the explicit classes of the elements of the list are different ([int,str,int,str,int,str]), and often that actually does matter for the use case, because int and str have different interfaces, and "adding" ints means something different than "adding" strs for example. It really just depends on what you're going to do with that data, and I think Guido was also talking in terms of the idiomatic use cases for the different data structures.
- dragonwriter 13y ago> This is true, and I'm sure Guido is fully aware of it Well, yeah. I'd be kind of stunned if he wasn't. I was making a note qualifying the examples that were given in isolation, not questioning the basic premise. > Regardless of inheritance, interfaces, and duck typing, the explicit classes of the objects still do matter. Whether or not that's true depends on the use case; the really relevant sense of homogenous or heterogenous that distinguishes the idiomatic use of tuples vs. lists is contextual, and is more about the interfaces that the consuming code expects the members to provide than the explicit classes per se. There's nothing unidiomatic in Python about using a list for a series of items of different explicit classes that are going to have the same operations (supported by a common interface they all share) performed on them.