0

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.

Margarita
  • 1,193
  • 2
  • 8
  • 12
  • Can you show the code you are trying to put in your interpreter that is giving you problems? We can't help without seeing the code in question. – idjaw Sep 26 '15 at 12:22
  • An empty line (two newlines) ends any statement in the IDLE console. – alexis Sep 26 '15 at 12:22

1 Answers1

1

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.

alexis
  • 48,685
  • 16
  • 101
  • 161