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

I have been using Python since 25 years, and never needed that one.



You've never had to read arbitrary json?


I 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.


Am I missing something, why wouldn't a non-homogeneous array literal pass?


Because mixing types in an array is stupid enough that I thought it might just not be valid.


Mixing 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.


That's an Optional[int]. It's not "mixed types", it's a union type.


That is merely a detail of how the typing module has decided to set up convenient aliases. Treated by the language (and likely compilers) as different types.


JSON 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.


You never implemented a quick polish notation calculator that uses this data structure? [1,2, '+']


I would use two stacks, one of which stores numbers and the other stores operators.


Why? Just to have no unions of types?


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: