3 ms·
> it would be nonsensical to write code which iterated over a list or tuple containing heterogeneous data. There are Python functions that take any type like r
by markrages 10y ago
> it would be nonsensical to write code which iterated over a list or tuple containing heterogeneous data.
There are Python functions that take any type like repr() or id()
- colanderman 10y agoSee my other comment; it does make sense to have a list of objects which share some interface. In the case you suggest, it is a very trivial and not-very-useful interface, but an interface nonetheless. You can do this with objects even in strictly-typed languages like OCaml or Mercury; code which iterates over the list is simply restricted to use only those methods which all elements have in common. Of course in languages which don't support a universal "object" type (say OCaml or C) you cannot do this -- numbers and strings have no interface in common. Those languages take advantage of this by not storing type information in collections (e.g. OCaml's float arrays, or any array in C), thus resulting in more compact memory footprints. If they instead allowed heterogeneous collections, the memory footprint of such collections would double.
- aangjie 10y ago> There are Python functions that take any type like repr() or id() Umm.. I haven't had to use any of them on a list(or for that matter on an object) for code.. (except when I'm curious and poking at python internals) May be I'm stuck in a web application role? Is it actually useful to say call repr() on a list of objects? May be call id to see if there's references to same object twice? I'm finding it hard to imagine cases where it would be needed to implement a feature, that wouldn't be better served by alternate methods.
- davrosthedalek 10y agoOne could argue that for those, the semantic differences between the types are dropped. In OO lingo, they look at the properties of the common base class.