前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python代码规范PEP 8——常见的规范错误与解决办法

python代码规范PEP 8——常见的规范错误与解决办法

作者头像
Twcat_tree
发布2022-11-22 09:23:50
3.1K0
发布2022-11-22 09:23:50
举报
文章被收录于专栏:二猫の家

文章目录

EPE 8规范

PEP 8: W191 indentation contains tabs

因为粘贴来的代码用tab缩进,而现在的代码用space当缩进 解决办法 方法一:Edit -> Convert Indents -> To Spaces 方法二:ctrl + shift + A => 在弹出的窗口中输入“To Spaces”就可以将所有的tab转为space 方法三:Code-> Reformat Code 先设置好缩进为space,一个Tab为4个space

PEP 8: W292 no newline at end of file

这里是要求你在末尾新起一行

PEP 8: W391 blank line at end of file

每个文件末尾应只有一个空白行,并且只有一个空白行。当空白行为零,两个或多于两个时,将发生此警告。

PEP 8: E203 whitespace before ‘,’

这个意思是逗号前面不能有空格

PEP 8: E231 missing whitespace after ‘,’

这个意思是逗号后面要有一个空格

PEP 8: E251 unexpected spaces around keyword / parameter equals

就是多了空格

PEP 8: E261 at least two spaces before inline comment

行内注释前需要两个空格

PEP 8: E262 inline comment should start with ‘# ’

行内注释应该以’#'加空格开始

PEP 8: E271 multiple spaces after keyword

关键字后空格多了

PEP 8: E302 expected 2 blank lines, found 1

就是希望有两个空行来区分

PEP 8: E305 expected 2 blank lines after class or function definition, found 0

这个是写完函数和类之后要空出两个空行(方便区分)

PEP 8: E271 multiple spaces after keyword

关键字后使用了多个空格,应该只使用一个空格

PEP 8: E302 expected 2 blank lines, found 0

有两个空白行,但是没有发现 在声明函数的那一行的上方必须有两行的空行,否则便出现这个情况。 现在只需要按照python的编码规范,在定义函数前空两行即可,问题解决。

PEP 8: E303 too many blank lines (2)

太多的行 这个规范对每行代码之间的间隔进行了定义,简单来说: 函数之间,类之间一般要空2行 类方法之间一般空1行 函数/方法代码内部每行间隔不超过1行

PEP 8: E402 module level import not at top of file

import不在文件的最上面,可能引用之前还有代码,把import引用放到文件的最上部就可以消除警告

关于缩进、多余空格、空行数量等相关的警告,均可使用yapf一键格式化(快捷键AIT+SHIFT+F) trailing whitespace:尾随空格,通常出现在语句结尾,一般删除句末空格即可。

multiple spaces after keyword:关键字后的多个空格,多于格式要求空格,删去。

indentation is not a multiple of four:缩进不是4的倍数,修改空格为偶数,2/4等。

missing whitespace around operator:缺少操作符周围的空格

行注释格式: #前两个空格,#后一个空格。 函数def编写格式:(expected 2 blank lines, found 1预计有2个空白行,找到1个) 函数首尾均空两行

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • EPE 8规范
    • PEP 8: W191 indentation contains tabs
      • PEP 8: W292 no newline at end of file
        • PEP 8: W391 blank line at end of file
          • PEP 8: E203 whitespace before ‘,’
            • PEP 8: E231 missing whitespace after ‘,’
              • PEP 8: E251 unexpected spaces around keyword / parameter equals
                • PEP 8: E261 at least two spaces before inline comment
                  • PEP 8: E262 inline comment should start with ‘# ’
                    • PEP 8: E271 multiple spaces after keyword
                      • PEP 8: E302 expected 2 blank lines, found 1
                        • PEP 8: E305 expected 2 blank lines after class or function definition, found 0
                          • PEP 8: E271 multiple spaces after keyword
                            • PEP 8: E302 expected 2 blank lines, found 0
                              • PEP 8: E303 too many blank lines (2)
                                • PEP 8: E402 module level import not at top of file
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档