Python PEP8 代码规范常见问题及解决方法

 PyCharm 写代码总是会出现波浪号,了解到 Python 的 PEP8 代码规范,所以将常见的 PEP8 代码规范问题和解决方法记录一下,养成良好的习惯,编写规范的代码!

PEP 8: expected 2 blank lines,found 1
解决方法:需要两条空白行,添加两个空白行即可,函数前后隔离需要两个空格!!!

PEP 8: Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click

鼠标选中单词-->点击鼠标右键-->spelling-->save 'xxx' to distionary

PEP 8: no newline at end of file
解决方法:代码末尾需要另起一行,光标移到最后回车即可

PEP 8: indentation is not a multiple of four
解决方法:缩进不是4的倍数,检查缩进

PEP 8: over-indented
解决方法:过度缩进,检查缩进

PEP 8: missing whitespace after’,’
解决方法:逗号后面少了空格,添加空格即可,类似还有分号或者冒号后面少了空格

PEP 8: multiple imports on one line
解决方法:不要在一句 import 中引用多个库,举例:import socket, urllib.error最好写成:import socket import urllib.error

PEP 8: blank line at end of line
解决方法:代码末尾行多了空格,删除空格即可

PEP 8: at least two spaces before inline comment
解决方法:代码与注释之间至少要有两个空格

PEP 8: block comment should start with ‘#’
解决方法:注释要以#加一个空格开始

PEP 8: inline comment should start with ‘#’
解决方法:注释要以#加一个空格开始

PEP 8: module level import not at top of file
解决方法:import不在文件的最上面,可能之前还有其它代码

PEP 8: function name should be lowercase
解决方法:函数名改成小写即可

PEP 8: missing whitespace around operator
解决方法:操作符(’=’、’>’、’<'等)前后缺少空格,加上即可

PEP 8: unexpected spaces around keyword / parameter equals
解决方法:关键字/参数等号周围出现意外空格,去掉空格即可

PEP 8: multiple statements on one line (colon)
解决方法:多行语句写到一行了,比如:if x == 2: print('OK')要分成两行写

PEP 8: line too long (82 > 79 characters)
解决方法:超过了每行的最大长度限制79

PEP 8: Simplify chained comparison
可简化连锁比较(例如:if a >= 0 and a <= 9: 可以简写为:if 0 <= a <= 9:)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值