Syntax error when defining a function on the Python command line

前端 未结 4 1602
感动是毒
感动是毒 2021-01-21 05:29

I am trying to define a function on the Python REPL. Every time I try to run the below code, I get a syntax error.

Code:

def hello():
    print (\"Hello!         


        
相关标签:
4条回答
  • 2021-01-21 05:42

    You should add a newline between defining function and calling it:

    def hello():
        print ("Hello!")
    
    hello()
    
    0 讨论(0)
  • 2021-01-21 05:45

    It looks like you've entered the whole block as a single statement. You'll need to hit enter after entering:

    def hello():
        print ("Hello!")
    

    So that the interpreter understands that this is a single definition that you've entered. Once that's been defined, then try running the hello() function.

    0 讨论(0)
  • 2021-01-21 05:46

    Press enter once after defining your function (that is, enter one blank line). Essentially, this lets Python know that you are done defining your function.

    Once you see >>> again, you can call your function.

    See the picture for how it should look when done right:

    0 讨论(0)
  • 2021-01-21 05:46

    If you want to understand why this is happening, rather than just learn a workaround that works for some mysterious reason:

    Interactive mode works by reading, compiling, and executing one statement at a time. This is a dead-simple rule—besides being dead-simple to implement it in the C code, it's also dead-simple to work through exactly in your head—once you understand it, that is. When given a choice between an implementation that's easy to explain, document, and work through in your head or one that's more complicated but sometimes easier to use, Python usually goes with the former.


    If you enter a simple one-line statement, that's easy: the line is a complete statement, so it just compiles and executes that line.

    But if you enter the first line of a compound statement—one that has a : on the end of the line and then an indented block—the statement isn't done until you write an indented block and then unindent again. So, it prints that special ... prompt instead of >>> to let you know it's continuing to read the same statement, and keeps going until you unindent.

    If you unindent by typing a blank line, the statement is done, so it compiles and executes it, and everything is good.

    If you unindent by typing a new one-liner statement, now it has two statements. If it tries to compile and run that as a single statement, that fails.

    By the way, there's nothing magical about def here; the same thing will happen with any compound statement:

    >>> for i in range(10):
    ...     print(i)
    ... print('Done')
    

    You'll get the same SyntaxError.


    Couldn't Python figure out that you gave it two statements, and compile and run them one after another? Yes. In fact, if you use IPython/Jupyter, it actually does exactly that. But this would make the rule more complicated—not to mention things like handling exceptions in the first statement. Python chooses the simple rule you can trace through in your head over the complicated rule here, even though the complicated rule would do what you want more often.


    So, how does this work in a module file? Well, modules aren't compiled a statement at a time, they're compiled all at once, as a list of statements, and then run all at once. That obviously doesn't work for the interactive terminal, because it can't run anything until it compiles everything, and it doesn't have everything until quitting time.


    One last thing: why do continued expressions work?

    >>> (1 +
    ...  2)
    3
    

    Expressions don't care about indentation. They end when all the parens/brackets/braces are balanced at the end of a line, not when the next line is unindented. So, as soon as you enter that 2), it knows the input is complete, and can compile it.

    And the same thing is true for backslash continuation:

    >>> 1 + \
    ...     2
    3
    

    No need for an unindented line; as soon as you type that 2 without a backslash continuation, the input is complete.

    0 讨论(0)
提交回复
热议问题