为什么Python语言没有writeln()方法?
如果我们需要向文件中写入新行,我们必须编写代码:
file_output.write('Fooo line \n')
Python 没有 writeln()
方法有什么原因吗?
If we need to write a new line to a file we have to code:
file_output.write('Fooo line \n')
Are there any reasons why Python does not have a writeln()
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Python 2 中,使用:
在 Python 3 中,使用:
In Python 2, use:
In Python 3, use:
省略了提供
file
方法的对称接口,因为writeln()
没有任何意义:read()
匹配write()
:它们都对原始数据进行操作readlines()
与writelines()
匹配:它们都对包含 EOL 的行进行操作readline( )
很少使用;迭代器执行相同的工作(除了可选的size
参数)writeline()
(或writeln()
)本质上是相同的为write()
,因为它不会添加 EOL(以匹配writelines()
的行为)。模拟
print
到文件的最佳方法是使用 Python 2.x 的特殊打印到文件语法或的
函数,就像 Daniel 建议的那样。file
关键字参数print()就我个人而言,我更喜欢
print >>file, ...
语法而不是file.write('...\n')
。It was omitted to provide a symmetric interface of the
file
methods and because awriteln()
would make no sense:read()
matcheswrite()
: they both operate on raw datareadlines()
matcheswritelines()
: they both operate on lines including their EOLsreadline()
is rarely used; an iterator does the same job (except for the optionalsize
param)A
writeline()
(orwriteln()
) would be essentially the same aswrite()
, since it wouldn't add an EOL (to match the behavior ofwritelines()
).The best way to mimic a
print
to a file is to use the special print-to-file syntax of Python 2.x or thefile
keyword argument of theprint()
function, like Daniel suggested.Personally, I prefer the
print >>file, ...
syntax overfile.write('...\n')
.我觉得应该是这样。您可以使用的最近的东西是:
I feel that it should. Nearest thing you can use is:
print()
是您正在寻找的函数。与 Python 中的许多核心多态性一样,打印是由内置函数提供的,而不是使用方法(就像len()
、next()
、repr() 等!)。
print() 函数作为通用接口也使其更加通用,而文件对象本身不必实现它。在这种情况下,它默认以换行符终止,但可以在函数调用中选择它,例如:
在您建议的用例中,所有文件对象不仅必须实现
.write()
方法(现在由print()
使用),还有.writeln()
,甚至更多!这种基于函数的多态性使 python 变得非常丰富,而不会给接口带来负担(记住鸭子类型是如何工作的)。注意:该模型一直是 Python 的中心。只是在我的例子中,与 Python 3 相关的例子更加纯粹
print()
is the function you are looking for. Like much of the core polymorphism in python, printing is provided by a built-in function instead of using methods (just likelen()
,next()
,repr()
etc!).The
print()
function being the universal interface also makes it more versatile, without the file objects themselves having to implement it. In this case, it defaults to terminating by newline, but it can be chosen in the function call, for example:In your suggested use case, all file objects would have to implement not only a
.write()
method (now used byprint()
), but also.writeln()
, and maybe even more! This function-based polymorphism makes python very rich, without burdening the interfaces (remember how duck typing works).Note: This model has always been at the center of Python. It is only more pure in my examples, that pertain to Python 3
可能没有真正的原因,只是在需要时编写换行符确实很容易,那么为什么要使用额外的方法来弄乱文件接口呢?
Probably no real reason, just that it's really easy to write a newline when you need it, so why clutter up the file interface with an extra method just to do that?