Python IDLE shell击键以指示语句结束并返回提示 [英] Python IDLE shell keystroke to signal end of statement and return to prompt

查看:141
本文介绍了Python IDLE shell击键以指示语句结束并返回提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在交互模式下,是否可以发信号通知语句结束(例如类定义)并返回提示以实例化对象?

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?

我经历了简单的练习-计算,ifs,循环和while语句.然后解释器得到"该声明已完成.

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

这似乎是一个简单的问题,但是无论是在stackoverflow还是在网络上搜索,我都没有运气.

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.

推荐答案

您可以在IDLE控制台中键入任何内容.函数和类定义(如循环)是多行语句.在IDLE提示符处(也在常规命令行python提示符处)空白行会终止一条语句.

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.

脚本和python提示符之间的主要区别是:

The main differences between scripts and the python prompt are:

a)在脚本中,函数或类的定义,循环,甚至一对括号的内部都可以包含空行;在IDLE控制台上,空白行将终止并执行一条语句.例如,您无法在IDLE提示符下成功键入以下内容:

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)IDLE控制台将打印在命令提示符处评估的任何表达式的值.在脚本中,您需要使用 print ,否则该值将消失.

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.

这篇关于Python IDLE shell击键以指示语句结束并返回提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆