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

This guy makes some statements about python that are completely and utterly wrong.

He implies python doesn't have an "if" expression. It does.

He specifically claims this is invalid in python implying that "if" expressions don't exist in python:

   x + (if is_true(): 1 else: 2)

The following is written in python and is correct syntax:

   x + (1 if is_true() else 2)
Although it's not "pythonic" to use python functionally, Python is a multi-paradigm language and has the facilities to be used as a very powerful functional language. It's not just a "functional" library. Functional is built into python syntax. The following is correct python syntax that will pass a type checker process as well.

   #List comprehensions using map and filter:
   x: List[int] = [i+2 for i in range(100) if i%2 == 0]
   #anonymous functions:
   x: Callable[[int], int] = lambda a: a + 2

   


With mypy, type annotations or any other external type checker, python approaches the power and correctness of typed functional programming languages like haskell or Ocaml, though it is missing many features.



The OP isn't trying to criticize Python... he was just trying to give an example of what it means for everything (in LISP) to be an expression. He didn't say that Python doesn't have an "if expression"... just that you can't use the if statement in an expression context.


OP literally says this:

In Python, an if condi­tional is a state­ment, and can only be used in certain posi­tions.

This is categorically wrong. In python the if conditional exists in statement form and expression form. The if-expression also basically exists in every other imperative language out there, usually in this form:

   x + (is_true() ? 1 : 2)
I know he's not trying to criticize python. Just want to correct his mistake and emphasize that python has intrinsic design features that can make it very very functional.


I think you just misinterpreted the point he's making. Yes, Python has an expression form for if, but it's a separate form. You can definitely write functional Python, but the point he makes is that Python differentiates between expressions and statements.


No I made no mistake. I get his point. Look at my reference. I literally posted the statement that is categorically wrong. He literally says that in python the if-conditional is a statement. This is False.

I also offer some proof as to why python is a bad choice for his examples because python is actually a highly functional language. It's like trying to use haskell to prove racket is more expressive. It's a bad choice.

Sometimes I don't understand people. I literally posted just factual errors, no opinions on his article and then people misinterpret everything I said as opinion and proceed to tell me I misinterpreted things, then vote me down. Seriously.


Yep. Also most c-derived languages have 'progn' as the comma operator. Expression-...ism? is not really a problem.


I don't understand, he seems to be dealing with that syntax like two lines later?

...

“But wait! Python has a ternary condi­tional expres­sion!” It doesn’t change the essen­tial point, but OK—you can indeed write this:

42 + (100 if 1 < 0 else 200)

But now suppose we want to use a condi­tional in place of the oper­ator, not the right-hand value. In a Lisp, because every­thing is an expres­sion—including the oper­ator itself—this is easy:

((if (< 1 0) + * ) 42 100)

But if you try the same thing in Python, it will raise a syntax error:

42 (+ if 1 < 0 else * ) 100

...


Hmm, my mistake I stopped reading after I saw he said something false. Guess I should read the whole thing instead of dismissing something after I see a false statement.


He mentions the expression form of 'if' right after the part you quoted. I'm not sure what you're trying to get at.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: