在 Python 交互式会话中禁用自动打印 [英] Disable automatic printing in Python interactive session

查看:61
本文介绍了在 Python 交互式会话中禁用自动打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将举办一个 Python 研讨会,在我的大学 Python 中教授一些初级程序员.

I am going to be holding a Python workshop to teach some beginner level programmers at my college Python.

我正在寻找一种方法来修改 Python 交互式会话的默认行为,以便表达式不会自动打印出结果.例如,采取以下交互式会话:

I am looking for a way to modify the default behavior of Python interactive sessions so that expressions do not automatically print out results. For example, take this following interactive session:

wil@calcifer:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 5
>>> y = 7
>>> x + y
12
>>> first_name = "Test"
>>> last_name = "Person"
>>> first_name + last_name
'TestPerson'
>>>

这将强制使用 print 语句以避免以后混淆.这并不重要,但我很惊讶我无法在任何地方找到任何设置.

This would enforce the use of the print statement to avoid confusion later on. This isn't crucial, but I was surprised that I was unable to find any settings for this anywhere.

我可以预先配置机器,因此任何解决方案都可以奏效.有谁知道这是否可行,或者交互式会话的哪些更改会产生这种效果?

I would be able to preconfigure the machine beforehand, so any solutions would work. Does anyone know if this is possible, or what change to the interactive sessions creates this effect?

推荐答案

我知道你来这里不是为了教学建议,但是没有输出,x + y 应该做什么?如果您不想将值输出或存储在任何地方,为什么还要键入它?

I know you didn't come here for teaching advice, but without an output, what's x + y supposed to do? Why type it at all if you don't want the value to be output or stored anywhere?

您的学生会感到困惑:x + y 是做什么的?"

Your students will be confused: "what does x + y do?"

如果你想显示一些表达式,只要在它前面始终放置一个print语句,或者给它赋值即可.

If you want to show some expression, just consistently put a print statement in front of it, or assign it to a value.

我发现人们经常反过来混淆,以至于他们不知道 return 实际上将值返还给他们,而他们总是想使用 打印.不要忘记您可以交互地定义函数,并将返回值放在那里.然后您可以向他们展示该函数返回该值,即使没有 prints.

I've found that people are often confused the other way around, so much that they don't know that return actually gives them the value back, and they always want to use print. Don't forget that you can define functions interactively, and put the return there. Then you can show them that the function returns that value, even with no prints.

这篇关于在 Python 交互式会话中禁用自动打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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