4 ms·
This is problem I have with Python in general; that even if you prefer to use it in a functional style, most libraries are written by real Python programmers, a
by leephillips 6y ago
This is problem I have with Python in general; that even if you prefer to use it in a functional style, most libraries are written by real Python programmers, and using them will force you to grapple with the object-oriented inversion of common sense (https://lee-phillips.org/pythonhate/ https://lee-phillips.org/pythonhate/).
My favorite example is: ','.join(['a', 'b'])
- creata 6y agoWhat's wrong with (','.join(['a', 'b']))? If it's the order of arguments, then I want to point out that the order has two big advantages: * partial application is actually useful: I've actually used (','.join) before, but (lambda sep: sep.join(['a', 'b'])) seems relatively useless. That's why Haskell also uses this order with (intercalate sep listOfStrings). * it accepts any iterable, rather than just lists.
- leephillips 6y agoWhat strikes me as weird about this is that our intent is to do something to the array of strings, so we are looking for a function that takes this array and transforms it into something else. But we are obligated to invert our intuition here, and write this as if we are doing something to the punctuation that we want to insert between the array elements. It’s backwards, and causes us (me, anyway) to keep forgetting how to use this method. I must have gotten this wrong a dozen times. The sensible way would be something like join(array, separator). That is straightforward, and would be hard to get wrong. It could even be written in a way where the order of arguments didn’t matter, so you wouldn’t even have to remember that. Why is the separator privileged as the implicit first argument of `join`? I accept that you’ve identified some advantages of the Python version; it’s always possible to find cases where object.method() notation gets you something.
- hakuseki 6y ago> It could even be written in a way where the order of arguments didn’t matter Well, 'hello'.join(', ') does not produce the same output as ', '.join('hello'), so the order of the arguments does matter.
- creata 6y agoPerhaps they want named arguments so that join(sep=', ', strings='hello') and join(strings='hello', sep=', ') are equivalent.
- leephillips 6y agoOK, but I was thinking along the lines of how easy it is in (for example) Julia to make methods that dispatch on types, so that your function could examine the arguments, and if one of them happens to be a single character, and the other a string or array, do the sensible thing, regardless of the order in which you typed in the arguments.
- blt 6y agoOOP interface is wrong for this functionality. It should be a free function. There is no reason it should be restricted to strings -- "join" makes sense on any sequence-like objects. We should be able to join(Iter1, Iter2[Iter3]) where Iter* are all arbitrary iterable data types.
- creata 6y agoI agree: you can definitely make it generic in both arguments. I was just arguing that this order of arguments makes more sense.
- fantod 6y agoYeah, the Python language favors an imperative style due to the absence of features necessary for writing more functionally, like lambdas that can do more than return an expression, tail call optimization, and lexical scope. This last one, additionally, tends to make people use classes for essentially no other reason than that closures don't work natively. > My favorite example is: ','.join(['a', 'b']) I've always found the discrepancy between this example (instance method call), list(('a', 'b')) (technically a method call, but resembles a functional call), and str.lower('AB') (class method call) to be quite maddening.
- NoThisIsMe 6y agoClosures do work natively in Python though. Lexical scope does not prevent that. Most languages, including functional languages, use lexical scope these days.
- eli_gottlieb 6y agoYou tried Coconut? http://coconut-lang.org/ http://coconut-lang.org/
- leephillips 6y agoI had not heard of that; it’s an interesting project. But of course it doesn’t address the issue I was talking about, which is the OO nature of existing libraries.