←back to thread

317 points est | 1 comments | | HN request time: 0.208s | source
Show context
amelius ◴[] No.17448876[source]
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 :)
replies(8): >>17448904 #>>17448927 #>>17448972 #>>17449048 #>>17449482 #>>17450517 #>>17450691 #>>17451251 #
bjoli ◴[] No.17449048[source]
I 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
replies(3): >>17449102 #>>17449137 #>>17450511 #
codetrotter ◴[] No.17449102[source]
I 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
replies(1): >>17449279 #
i_do_not_agree ◴[] No.17449279[source]
Perhaps 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.
replies(2): >>17449565 #>>17452255 #
codetrotter ◴[] No.17449565[source]
Yeah 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

https://github.com/hylang/hy

replies(1): >>17452537 #
bjoli ◴[] No.17452537[source]
He/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.

replies(1): >>17453913 #
codetrotter ◴[] No.17453913[source]
> I don't know what you would lose by having if as an expression

Well, if you turn if into an expression the way that you indicated then now you will also need the equivalent of the “begin” expression in order to be able to have multiple statements and/or expressions in either branch.

So then you are breaking backwards compatibility. Which makes it a non-starter from the get go.

And like I said there is also the fact that functions need the return keyword in Python if you want to return a value.

replies(1): >>17461450 #
1. bjoli ◴[] No.17461450[source]
You could have the same looks as the regular if, but have it implicitly return a value. I was just too sleep deprived to be able to type out correct syntax on my phone.

Ruby returns the last thing in a method, which I feel is pretty sane.