Search code examples
pythoninteractivekeystrokes

Python IDLE shell keystroke to signal end of statement and return to prompt


In interactive mode, is there a way to signal the end of a statement (such as a class definition) and return to the prompt in order to then instantiate objects?

I've gone through simple exercises - calculations, ifs, loops, while statements. And the interpreter "gets" that the statement is complete.

It seems a simple question, but I've had no luck searching either in stackoverflow or the web generally.

(More generally, are there limitations re: what you can do in interactive mode vs via a script. Or should one be able, in theory, to experiment with all aspects of the language?) Thanks.


Solution

  • You can type anything in the IDLE console. Function and class definitions, like loops, are multi-line statements. A blank line at the IDLE prompt (also at the regular commandline python prompt) terminates a statement.

    The main differences between scripts and the python prompt are:

    a) In a script, a function or class definition, a loop, or even the inside of a pair of parentheses can contain empty lines; on the IDLE console, a blank line will terminate and execute a statement. E.g., You can't successfully type the following at the IDLE prompt:

    def something():
        x =0
    
        return x
    

    b) The IDLE console will print the value of any expression evaluated at the command prompt. In a script, you need to use print or the value will disappear.

    >>> 2 + 2
    4
    

    Note for completeness: A blank line will not terminate a syntactically incomplete statement (e.g., unmatched parentheses). But never mind that.