2

maybe I'm missing something very simple ? However I can't see the reason why if I paste the following code in a python shell :

a = 0
if True:
    a += 1
a += 2

I get this error:

a += 2
^ SyntaxError: invalid syntax

However if I put a newline before the a += 2 line, the statement works.

Same behaviour in both Python 2 and 3, but here is no error if I launch the code from a file.

Is there some python rule I'm missing ?

Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67
  • 3
    Yes the original `python` REPL requires a new line after each indented block, while the `ipython` REPL does not. – Matteo Ragni Oct 18 '17 at 07:58
  • Thanks, I had no idea. So it's actually a bug / feature / limitation of the REPL rather than python syntax. – Eino Gourdin Oct 18 '17 at 08:05
  • 1
    This is actually due to the fact that the original REPL does not require additional libraries to work (for example it does not use `GNU readline`) while other REPL contains additional libraries to support that "simple" feature (`ipython` runs with `prompt_toolkit`, a pure python replacement for `readline`). This is not a bug nor a feature, I think it is simply the result of the decision of limiting the number of dependencies for the compilation of the python interpreter. – Matteo Ragni Oct 18 '17 at 08:14

0 Answers0