7 ms·
I have been using Python since 25 years, and never needed that one.
by jnxx 4y ago
I have been using Python since 25 years, and never needed that one.
- joshmaker 4y agoIn 25 years you’ve never once created a list with more than one type of object in it?
- cjohnson318 4y agoMaybe a list with ThingObject or None, but my lists are usually homogenous.
- mrfox321 4y agothats just homogenous Sequence[Optional[T]], though.
- bombolo 4y agowell list[int|str] is as well
- cjohnson318 4y agoYeah, 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.
- agumonkey 4y agoAnd everything is a subtype of Any. The usual 'static typing' of dynlangs.
- mangecoeur 4y agoI have also been using python a long time and i honestly can’t remember a time i used mixed type list.
- querez 4y agoIn my code this comes up often, e.g. when I use tuples instead of namedtuple or a dataclass.
- bastawhiz 4y agoTuples aren't lists though. Tuples are really just structs with indexes instead of names.
- froggit 4y agoI use mixed lists all the time to store shit, but I'm also a total python newb that really doesn't know better.
- ris 4y agoNever have to handle/forward function calls with arbitrary arguments? What do you think *args is?
- mangecoeur 4y agoa tuple
- gautamdivgi 4y agoI’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.
- throwaway894345 4y agoI 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”.
- bombolo 4y agoIt'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.
- terhechte 4y agoSame here. If I needed that I might use a tuple.
- amelius 4y agoAnd then it crashes when you convert it to JSON.
- UncleEntity 4y agoI 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.
- eddsh1994 4y agoHuh, thinking about it I haven't in 9 years either
- ris 4y agoYou've never had to read arbitrary json?
- insanitybit 4y agoI just did a quick check (apparently DuckDuckGo has a built in JSON validation function lol love it) and `[1, "foo"]` does pass their JSON validation. Not surprised but I did want to confirm.
- okayexplainl4 4y agoAm I missing something, why wouldn't a non-homogeneous array literal pass?
- insanitybit 4y agoBecause mixing types in an array is stupid enough that I thought it might just not be valid.
- ris 4y agoMixing types in an array/list is not remotely stupid. How exactly would you suggest expressing the lack of a value in a series of numbers without None/null? There's your mixed types. Don't let weird dogma get in the way of practicality.
- insanitybit 4y agoJSON has a `null` value already, and null is its own special case in most languages where `null` is a universal type. Otherwise you tag your data so that it's an array of objects.
- qwefsdf 4y agoThat's an Optional[int]. It's not "mixed types", it's a union type.
- bombolo 4y agoYou never implemented a quick polish notation calculator that uses this data structure? [1,2, '+']