8 ms·
If they add anything to Python, it should be the ability to do functional-style programming without hassle. Right now it's almost impossible to compose function
by amelius 8y ago
If they add anything to Python, it should be the ability to do functional-style programming without hassle. Right now it's almost impossible to compose functions in an inline style such as used in functional programming languages. Yes, there's lambda, but it doesn't extend into multiple lines, and applying a function to a bunch of lambdas directly leads to line-width overflow. Even JavaScript has better support for functional-style programming. Perhaps Guido should spend a year writing Haskell :)
- icebraining 8y agoPython has explicitly discouraged functional-style for a long time, I don't think that's going to change. And frankly, the whole language is quite, I'd even say extremely, mutable; I'm not sure a functional syntax works well with that.
- aib 8y agoThe problem is, python list comprehensions are very... specific. Add to that the brokenness of if...else and you get a very quirky and specific sublanguage that is hard for outsiders to read. And this is before we take into account the locals/scope/clojure discussion in the link and the PEP (which I frankly did not bother to read.) On the other hand, you have a concept which translates easily to any language with first-class functions and lambdas. Even the syntax stays the same among languages which use "f(x,y)" for function evaluation and parameter passing. /* This post is for those occasions when a list comprehension style is advocated over a functional style, which I know was not necessarily what you were doing in your comment. But I think the two points are valid enough on their own. */
- kstrauser 8y ago> Add to that the brokenness of if...else Could you elaborate on that?
- aib 8y ago> > Add to that the brokenness of if...else > Could you elaborate on that? It's a very opinionated statement on my part. `if COND then TRUE-CASE else FALSE-CASE` is the correct form to use, in my opinion. Python uses `TRUE-CASE if COND else FALSE-CASE`.
- hellofunk 8y agoConventional if statements in Python do use your preferred form of `if COND then TRUE-CASE else FALSE-CASE`. What you are talking about is a different kind of expression, similar to a ternary operator. It is not the same as if...else
- aib 8y agoThe reason it's not the same as if...else is that it's freaking backwards :D
- hellofunk 8y agoYou said: > Python uses `TRUE-CASE if COND else FALSE-CASE`. And this is wrong -- this is not how Python if statements work.
- aib 8y ago> > Python uses `TRUE-CASE if COND else FALSE-CASE`. > And this is wrong -- this is not how Python if statements work. Huh? Didn't you yourself say I was not talking about if statements: > What you are talking about is a different kind of expression, similar to a ternary operator. It is not the same as if...else In any case, I'm talking about the case that goes `TRUE-CASE if COND else FALSE-CASE`, as can be deduced from my typing `TRUE-CASE if COND else FALSE-CASE`.
- laumars 8y ago> Even JavaScript has better support for functional-style programming. Which shouldn't be that surprising considering originally Netscape were going to port Scheme to their browser before choosing to create a new scripting language with "Java-like syntax" (you can argue amongst yourselves just how Java-like the syntax really is).
- beojan 8y ago> originally Netscape were going to port Scheme to their browser Imagine where we'd be if they'd just done that.
- laumars 8y agoProbably no better off as I cannot see many frontend developers taking to Scheme like they had with Javascript. So we'd still have eventually ended up with something nasty just to appease the lowest common denominator. Here's a horrible thought: maybe VBScript would have become the de facto standard instead?
- rthomas6 8y agoYeah that's true, nobody uses Clojurescript or anything.
- laumars 8y agoYou have to bare in mind that we - on HN - are inside a niche bubble and thus the kind of developers that read HN are very different to the kind of developers in many of the web shops outside of the big tech hubs like Silicon Valley and London. There are a hell of a lot of frontend devs who do still like to keep Javascript at arms length and a lot of "web developers" who are basically just Wordpress themers. I know this for a fact because I've worked with a great many of them before moving closer to London. These people are obviously still competent at what they do since not ever website nor development job requires "superstars" and the web framework - for all it's faults - offers a low enough barrier to entry that anyone can throw a page together. Including even those who don't do dev for a living. In fact one of the reasons the web took off was because of how easy it was for anyone to throw together a homepage. Had Scheme been Netscapes scripting language instead of Javascript then I could easily see many of the less dedicated developers and hobbyists getting frustrated at S-expressions and such like. I mean I love functional programming but even I cannot deny that the learning curve is steeper and S-expressions are less readable (at least to an untrained eye) than Javascript is. So my point was if Javascript didn't exist then I suspect there would be enough demand to either dumbdown / bastardise Scheme, or implement another scripting language which was more hobbyist friendly (also hence the VBScript quip).
- i_do_not_agree 8y agoJust need something like blocks in Smalltalk. Wikipedia page on list comprehensions says Smalltalk-80 had list comprehensions and that was ~ 40 years ago. Smalltalk also uses ":=" for assignment and "=" for comparison. In Pharo, VA and Dolphin at least does what this Python proposal does - return the value of the last expression.
- yxhuvud 8y agoWell, if you prefer blocks, you may as well use Ruby as that language is basically what happens when you have access to blocks. Python chose a different design trajectory - personally I can't stand it but it certainly follow some sort of internally consistent reason.
- jrochkind1 8y agoThere's a whole lot of smalltalk in ruby.
- yxhuvud 8y agoAin't going to happen. Python used to have lambdas, but removed all but the one-line variety. The problem is that if you add blocks, then half of the added syntactic features the last decade is redundant as a block version would simply solve the problem a better. Which would create a lot of dead design, and that makes it a bad solution.
- pseudalopex 8y agoWhen did Python have multi-line lambdas? Are you maybe conflating blocks with chained iterator operations? Adding blocks to Python's current functional syntax would be pretty ugly. "foo.filter(bar).map(baz)" is nice even if you can only use named functions.
- dfox 8y agoSmalltalk (and for that matter Ruby) has weird feature that blocks and methods are different different concepts. In my opinion the Python's explicit self argument is somehow cleaner approach than having distinct block and function/method types. You still need some kind of ugliness in order to implement super(), but for Python 3 that happens at compile time and the resulting syntax is reasonably sane. As for the aforementioned method context issue CLOS/MOP takes interesting approach of macroexpanding the method definition into something like (lambda (args next-method) (labels ((call-next-method &args) ... (funcall next-method ...))) (impl (...) ... method body ...)) (funcall impl args))) Also of note is that in ST, there are no control structures, even if is implemented as method on Boolean instances which takes block argument, with true and false being instances of True resp. False with different implementations of #ifTrue: method.
- bjoli 8y agoI wonder what would break if they started treating if as an expression. It is much nicer to do: def foo(X): if bar(foo): 1 False
- codetrotter 8y agoI don’t understand what you are trying to write. First of all you have indented 1 and False equally. Is that a typo? Or is it your opinion that the if should always consist of the if and the else branch without using the else keyword? Secondly, if you want to return a value you need to use the return statement. Also you wrote bar(foo) but foo was the name of the function, not the name of your parameter. Perhaps what you are looking for is this: def foo(x): return 1 if bar(x) else False
- i_do_not_agree 8y agoPerhaps s/he was looking at Lisp/Scheme: (define (foo x) (if (bar x) 1 #f)) The returned value is the value of the last expression. No need for an else, or a return keyword.
- codetrotter 8y agoYeah I considered that but it doesn’t make much sense to change Python to be like that. It’s fine like that in Scheme and the other Lisps in part because well that’s the way they always did it, but it’s quite different from how it is and has been in Python. If they want Lisp in Python they should look into Hy. http://docs.hylang.org/en/stable/quickstart.html http://docs.hylang.org/en/stable/quickstart.html https://github.com/hylang/hy https://github.com/hylang/hy
- bjoli 8y agoHe/she was right, I was trying to write scheme python. I have spent quite some time writing python, but my sleep deprived brain wants to make everything into scheme :) I don't know what you would lose by having if as an expression. It is easy to notice when it is used in expression context, and there is no extra computation that needs to be done. It was sort of addressed with the trenary operator, but that quickly becomes ugly.
- sametmax 8y agoHaskell has always officially inspired Python tooling. But Python does things in it's own way, and they are not random, they are the result of an opinion. First class citizen functions, short lambdas, comprehension lists, generators, map(), filter(), itertools, operator and functools are quite a rich toolbox already. But you won't have more. It's a choice. The idea is to have enough to be productive, and not enough to be dogmatic. The experience of Guido, and it's one that I share, is that too much functional tooling drives a style that favors expressive writing at the expense of ease of reading. It's not by chance that LISP and Haskell are considered hard languages to get into, while Python is considered easy to start with. It has a cost, since no language is perfect, but that's the path this language follows and requesting a snake to fly will only bring you disappointments. Python tries to strike the balance between the importance of a rich expressiveness and the non negotiable necessity of keeping the code readable: you read a line much more often that you write it, after all. It's a key philosophy of the language. It shaped and will shape numerous decisions around it. This PEP is a perfect example : it tooks years for the concept to be integrated in Python, and the last debate about this concrete implementation took months. The result is a carefully crafted feature with a lot of details to discourage abuse and remove the needs for pondering when to use it or not.
- nikofeyn 8y agoyou are right that python is an opinionated choice, but that particularly chosen philosophy is what people disagree with. the philosophy is somewhat stubborn as well. and i think you are leaving out functional languages which share python's readability, if not surpass it, while remaining much more expressive. that's f# and ocaml. f#, in my opinion, is superior in everyway to python and subsumes python's abilities of readability, easiness, oop, and scripting, while greatly raising the ceiling of possibility. it's criminally underused, especially in areas where python has been chosen. and i disagree lisp is harder to get into. racket is just as easy to learn as python, if not easier, due to its regularity. the how to code / systematic program design course on edX and the book how to design programs showcases this.
- sametmax 8y agoI heard good things about F# and I did like C#, so I wanted to give an honest look at your arguments. The first thing I checked is your very vocal assurance that F# is a better scripting language than Python. That seemed very weird to me, after all it's Python strong point. Since I script a lot, I looked for the most popular F# lib to parse script arguments. Argu seems the winner, according to http://fsharpworks.com/survey.html http://fsharpworks.com/survey.html. Their tutorial is pretty good (https://fsprojects.github.io/Argu/tutorial.html https://fsprojects.github.io/Argu/tutorial.html), and here is their hello world. 24 lines of, packing a dense symbology and using a lot of the specific language features: open Argu type CLIArguments = | Working_Directory of path:string | Listener of host:string * port:int | Data of base64:byte[] | Port of tcp_port:int | Log_Level of level:int | Detach with interface IArgParserTemplate with member s.Usage = match s with | Working_Directory _ -> "specify a working directory." | Listener _ -> "specify a listener (hostname : port)." | Data _ -> "binary data in base64 encoding." | Port _ -> "specify a primary port." | Log_Level _ -> "set the log level." | Detach _ -> "detach daemon from console." let parser = ArgumentParser.Create<CLIArguments>(programName = "gadget.exe") let results = parser.Parse [| "--detach" ; "--listener" ; "localhost" ; "8080" |] printfn "%A" results.GetAllResults();; The same thing with click, the Python most popular solution, is 11 lines, and it's shaped around almost only regular calls and parameters: import click as cli, base64, urllib.parse as url @cli.command("gadget.exe") @cli.option('--working-directory', help='specify a working directory.', type=cli.File('rb')) @cli.option('--listener', help="specify a listener (hostname : port)", type=url.urlparse) @cli.option('--data', help='binary data in base64 encoding.', type=base64.b64decode) @cli.option('--port', help='"specify a working directory.', type=cli.File('rb')) @cli.option('--log-level', help='set the log level.', type=int) @cli.option('--detach', is_flag=True, help='detach daemon from console') def hello(**kwargs): print(kwargs) hello(["--detach", "--listener", "localhost:8080"]) I have a hard time finding the motivation to look for the truth behind your other arguments after that.
- Myrmornis 8y agofrom toolz import compose
- olavk 8y agoThere are plenty of very good functional programming languages. If you prefer Haskell to Python, just go ahead and use Haskell. Python has a very specific design philosophy which is a focus of readability and the "preferably only one obvious way of doing it".
- amelius 8y agoIt's not easy to "just" switch languages. You also have to consider the ecosystem built around them. For example, I love NumPy and SciPy. > preferably only one obvious way of doing it I don't think that is true at all. Python allows you to perform many tricks, with overloading operators etc.
- olavk 8y agoYou should probably look into the JVM or .net platforms, where the same libraries are available through multiple languages. For example .net have F# which is a very strong functional language, and the JVM have Scala and Clojure. Pyhton is not really that kind of language.
- zorkw4rg 8y agoHow a language with nested list and dict comprehensions can ever claim to focus on readability is beyond me, or look at typing and the mess that leaves behind. One obvious way of doing things while simultaneously mixing oop and functions arbitrarily in their standard library even providing camelCase and snake_case aliases(!) for parts of it. And why does the base64/b64encode return bytes and hashlib/hexdigest str? Python is designed in such a strangly arbitrarily inconsistent, hypocritical and opinionated manner.
- toyg 8y ago> even providing camelCase and snake_case aliases(!) Sorry, but it's preposterous to bring up an implementation detail of the standard library that has long been in the process of being fixed. CamelCase was deprecated a long time ago in favour of snake_case; what is left of CC is for backward-compatibility and will eventually disappear. This is all documented. The standard library is not the language, it's much messier and suffers from all sorts of issues that have nothing to do with the language itself.
- roerdhh 8y agoIt feels to me that the use of lambda is pretty much discouraged in Python. The preferred way to dynamically create functions seems to be an inner def.