Python 脚本是否应该以新行结尾?Pylint自相矛盾? [英] Python script should end with new line or not ? Pylint contradicting itself?

查看:100
本文介绍了Python 脚本是否应该以新行结尾?Pylint自相矛盾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Pylint 的新手,当我针对我的脚本运行它时,我得到以下输出:

I am new to Pylint, and when I run it against my script, I get this output:

C: 50, 0: 尾随换行符(trailing-newlines)

这里,Pylint 是说最后一个换行是不好的.

我喜欢在我的脚本末尾有一个新行,所以我想我应该禁用这个警告.我做了一些谷歌网络搜索,发现了这个:http://pylint-messages.wikidot.com/messages:c0304

I like to have a new line at the end of my scripts, so I thought I would disable this warning. I did some google web searching and found this: http://pylint-messages.wikidot.com/messages:c0304

C0304 消息

缺少最后的换行符

说明

当 Python 源文件上没有行结束符时使用最后一行.

Used when a Python source file has no line end character(s) on its last line.

此消息属于格式检查器.说明

This message belongs to the format checker. Explanation

虽然 Python 解释器通常不需要行尾最后一行的字符,处理 Python 源代码的其他程序文件可能会这样做,拥有它只是一种很好的做法.这是在 Python 文档中确认: Line Structure 表明物理行以平台的相应行结束字符结束.

While Python interpreters typically do not require line end character(s) on the last line, other programs processing Python source files may do, and it is simply good practice to have it. This is confirmed in Python docs: Line Structure which states that a physical line is ended by the respective line end character(s) of the platform.

这里,Pylint 是说错过最后一个换行符是不好的.

(A) 正确的观点是什么?(B) 如何禁用对最后一行的检查?

(A) What is the correct view ? (B) How do I disable the check for the final new line ?

{{ 事实证明这不是 Pylint 的问题;这是 vim 的一个问题,它会自动添加 eol:VIM 禁用自动换行在文件末尾 }}

{{ EDIT : It turns out that this is not an issue with Pylint ; It is an issue with vim , which adds eol automatically : VIM Disable Automatic Newline At End Of File }}

推荐答案

您收到的 pylint 警告是抱怨您有多个尾随换行符.当完全没有尾随换行符时,会出现 C0304 消息.

The pylint warning you are getting is complaining that you have multiple trailing newlines. The C0304 message occurs when there is no trailing newline at all.

这些消息并不矛盾,它们表示不同的问题.

These messages are not contradictory they indicate different problems.

您需要至少一个换行符的原因是,如果文件结束并且最后一行上有文本但文件末尾没有换行符,那么历史上某些工具会出现问题.写得不好的工具可能会错过最后一部分行的处理,更糟糕的是可能会读取最后一行之后的随机内存(尽管用 Python 编写的工具不太可能发生这种情况,但用 C 编写的工具可能会发生这种情况).

The reason you need at least one newline is that historically some tools have problems if the file ends and the last line has text on it but does not have a newline at the end of the file. Badly written tools can miss processing that last partial line, or even worse can read random memory beyond the last line (though that is unlikely to happen with tools written in Python it can happen with tools written in C).

所以你必须确保有一个换行符来终止最后一个非空行.

So you must ensure there is a newline terminating the last non-blank line.

但是您也不希望文件末尾出现任何完全空白的行.它们实际上不会产生错误,但它们不整洁.所以删除任何空行,你应该没问题.

But you don't want any completely blank lines at the end of the file either. They won't actually produce bugs but they're untidy. So delete any blank lines and you should be fine.

这篇关于Python 脚本是否应该以新行结尾?Pylint自相矛盾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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