print() 函数是否算作标准输出,而 input() 函数是否算作标准输入?

发布于 2025-01-14 05:20:42 字数 322 浏览 2 评论 0原文

上周,我报名参加了逆向工程编码竞赛。规则规定“程序的所有输入和输出都必须通过标准流(分别为 stdin 和 stdout”)。”读完后,我查阅了他们的官方实践 测试并开始编写我的代码。因为我不熟悉 sys.stdin.read 和 sys.stdout.write 函数,所以昨晚我在研究改进代码的方法时偶然发现了一个编码。博客指出input() 函数实际上是 stdin 的一种形式。我在互联网上进行了更多研究,发现另一家公司的一个单独的博客说的是相对相同的事情,那么……这是真的吗? print() 是我可以用于比赛的 stdin 和 stdout 形式吗?

Last week, I signed up for a reverse-engineering coding competition. The rules stated "All input and output to the program must be through the standard streams (stdin and stdout, respectively")." Upon reading that, I looked up their official practice test and started writing my code. I encountered many roadblocks because I was unfamiliar with the sys.stdin.read and sys.stdout.write functions. Then last night while I was researching ways to make my code better, I stumbled upon a coding blog stating the input() function is in fact a form of stdin. I looked around even more on the internet and came across a separate blog from another company that said relatively the same thing. So... is this true? Is input() and print() a form of stdin and stdout I can use for my competition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦浮鱼 2025-01-21 05:20:42

是的,实际上 print 函数使用 sys.stdout 作为其 write() 方法。

在Python文档中,您可以发现打印函数的定义如下:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

哪个文件参数必须是具有write(string)方法的对象。如果它不存在或无,则将使用 sys.stdout。因此默认情况下打印使用 sys.stdout。

同样对于输入函数,你可以在文档中找到这句话:

stdin 用于所有交互式输入(包括对 input() 的调用)。

Yes, actually the print function uses sys.stdout as its write() method.

In the python documentation, you can find that the print function is defined by:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

which file argument must be an object with a write(string) method. If it is not present or None, sys.stdout will be used. So by default print uses sys.stdout.

Also for the input function, you can find this sentence in the documentation:

stdin is used for all interactive input (including calls to input()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文