3 ms·
> Ruby and Python do not have statements - only expressions. Unfortunately this is not true for python. The reason his print-example doesn't show the returned
by bajsejohannes 15y ago
> Ruby and Python do not have statements - only expressions.
Unfortunately this is not true for python. The reason his print-example doesn't show the returned value is that there isn't one. In ruby, puts actually returns Nil, and we can assign it:
>> x=puts(0)
0
=> nil
Whereas in Python, we get a syntax error
>>> x=print(0)
File "<stdin>", line 1
x=print(0)
^
SyntaxError: invalid syntax
- andrewcooke 15y agothat's fixed in python 3 [edit: sorry, yes, i agree the general problem remains.]
- bajsejohannes 15y agoFor the specific example of print, then yes. But in general it is not. Another example is if-statements in python: >>> x=if True: File "<stdin>", line 1 x=if True: ^ SyntaxError: invalid syntax whereas in Ruby: >> x=if true: ?> 5 >> end => 5 Other Python statements from the top of my head: for, while, def, import. My point is that there absolutely are statements that are not expressions in Python. (You can easily tell by trying to assign the return value: x=<statement>).
- kbd 15y agoHeck, assignment itself is a statement. That's why you can't say while x=y: ... (and you'll notice the REPL gives no return value for an assignment). This "Python has no statements" error was one of my biggest annoyances with this article, as Python has a very strong statement vs expression dichotomy.
- deleted 15y ago[deleted]
- lloeki 15y agoBarring the fact that print became a function in Python 3 (and in Python 2.6+ with `from __future__ import print_function`), Python functions actually `return None` when the end of scope is reached, and indeed Python REPL does not show `None` being returned. >>> def a(): ... pass ... >>> a() is None True >>> a() >>>