3 ms·
Python libraries kind of used to do this more, but seem to be doing it less now, in part because highly-polymorphic interfaces are a huge ugly pain in the butt
by nerdponx 4y ago
Python libraries kind of used to do this more, but seem to be doing it less now, in part because highly-polymorphic interfaces are a huge ugly pain in the butt with the Python type hinting system.
- int_19h 4y agoWhat's so painful about writing foo: int | str
- nerdponx 4y agoIt gets a lot more complicated than that. One of many, many examples: https://github.com/pandas-dev/pandas-stubs/blob/v1.4.3.220822/pandas-stubs/core/series.pyi#L118-L151 https://github.com/pandas-dev/pandas-stubs/blob/v1.4.3.22082... In general, if someone with experience in a domain is complaining that something is hard, it's safe to assume that the thing is actually at least kind of hard, and that they aren't just a crybaby about indentation and formatting.
- int_19h 4y agoI don't see what's complicated about this example. It adds overloads, but those aren't hard, either.
- nerdponx 4y agoFair, it's not "hard". But I stand by my assertion that it's a huge ugly pain in the butt. That doesn't mean it's not worth doing, but that doesn't make it less huge, ugly, or painful. I also assert that if NDFrame.loc were less polymorphic (e.g. if you had a separate accessor like NDFrame.mask for boolean subsetting) it would be easier to use, easier to reason about, easier to teach, easier to document, and easier to write type hints for. The worst situation is when you want to have multiple return types depending on the argument values, like Pandas' inplace=True. Putting aside the problem of dependent types, even supporting only literal True and False requires you to write out every single overload twice, creating a literal combinatorial explosion of overloads. And that's also ignoring the whole world of parametric polymorphism required for higher-order functions, factory functions etc. Maybe the situation is no better in something like C++, but I don't know enough C++ to have an opinion on it.
- fshr 4y agoTo be fair, there are few things as here-be-dragons as the guts of Pandas. It'd be hard to find a more rarified example, but it served to further the conversation at least.
- UncleEntity 4y ago> Python libraries kind of used to do this more, but seem to be doing it less now… I’d argue that it has to do with C extensions making it a lot of work to get it right. Some library code does it (which is where I learned how to do it ‘correctly’) but in general, run of the mill code it is pretty rare. Just something like an overloaded __add__ turns into a page and a half of code and there’s a lot of functions in the numbers protocol struct. Labor of love really.