Hacker News new | past | comments | ask | show | jobs | submit login

In 25 years you’ve never once created a list with more than one type of object in it?



I mean, it depends on what you mean by “type”. A list of some Protocol type (what other languages call “interfaces”) is still a homogenous list even though the concrete types are heterogeneous. This is almost always what you want when you’re thinking of “a heterogeneous list”.


It's not unheard of to have unions and a couple of if isinstance.

In fact it's why in python they even have the | operator between types nowadays.


I have also been using python a long time and i honestly can’t remember a time i used mixed type list.


In my code this comes up often, e.g. when I use tuples instead of namedtuple or a dataclass.


Tuples aren't lists though. Tuples are really just structs with indexes instead of names.


Never have to handle/forward function calls with arbitrary arguments? What do you think *args is?


a tuple


I use mixed lists all the time to store shit, but I'm also a total python newb that really doesn't know better.


Maybe a list with ThingObject or None, but my lists are usually homogenous.


thats just homogenous Sequence[Optional[T]], though.


Yeah, that's a better way of putting it :) 90% of the time, I'm something like Sequence[T], but I'm sure I've used Sequence[Optional[T]] a couple of times. I mean, I could drive donuts in the Piggly Wiggly parking lot at 3a, I just don't, and the same for heterogenous lists.


And everything is a subtype of Any. The usual 'static typing' of dynlangs.


well list[int|str] is as well


I’ll second that. I’ve been doing python for a while and haven’t used the mixed type list. I’ve actively avoided doing something like that. The situation doesn’t come up often.


Same here. If I needed that I might use a tuple.


And then it crashes when you convert it to JSON.


I just use an extension wrapper around boost::property_tree for my json (and xml) needs. Way faster than the built in json support and does automagic type conversion so I don’t have to worry about it.

Now, I’m not running at Web Scale™ but pure python was slow enough to be annoying.


Huh, thinking about it I haven't in 9 years either




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: