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

news2025/4/10 23:26:30


Win11查看安装的Python路径及安装的库

Python3(基础|高级)语法实战(|多线程|多进程|线程池|进程池技术)|多线程安全问题解决方案

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

Python3操作MySQL8.XX创建表|CRUD基本操作

Python3操作SQLite3创建表主键自增长|CRUD基本操作

anaconda3最新版安装|使用详情|Error: Please select a valid Python interpreter

Python函数绘图与高等代数互融实例(一):正弦函数与余弦函数

Python函数绘图与高等代数互融实例(二):闪点函数

Python函数绘图与高等代数互融实例(三):设置X|Y轴|网格线

Python函数绘图与高等代数互融实例(四):设置X|Y轴参考线|参考区域

Python函数绘图与高等代数互融实例(五): 则线图综合案例


一: Python 的 EP8 代码规范P简介

所以将常见的 PEP8 代码规范问题和解决方法记录一下,学习一下,遇到了再持续更新,养成良好的习惯,编写规范的代码!


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: expected 2 blank lines,found 0
解决方法:需要两条空白行,添加两个空白行即可

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:)

如果想要选择性忽略PEP8代码风格的警告信息可以使用以下方法:(养成良好的习惯,编写规范的代码!不推荐忽略!)

①将鼠标移到出现警告信息的地方,按 alt+Enter,选择忽略(Ignore)这个错误即可:

②依次选择 File - Settings - Editor - Inspections,在 Python下找到 PEP8 coding style violation 选项,在右下角的 Ignore errors 里点击加号可以添加需要忽略的警告信息ID(ID信息见后面附录),例如想要忽略indentation contains mixed spaces and tabs这个警告,只需要添加其ID:E101 即可


附录:全部警告信息以及对应的ID,官方地址:https://pep8.readthedocs.io/en/latest/intro.html#error-codes

code    sample message
E1    Indentation
E101    indentation contains mixed spaces and tabs
E111    indentation is not a multiple of four
E112    expected an indented block
E113    unexpected indentation
E114    indentation is not a multiple of four (comment)
E115    expected an indented block (comment)
E116    unexpected indentation (comment)
E117    over-indented
E121 (*^)    continuation line under-indented for hanging indent
E122 (^)    continuation line missing indentation or outdented
E123 (*)    closing bracket does not match indentation of opening bracket’s line
E124 (^)    closing bracket does not match visual indentation
E125 (^)    continuation line with same indent as next logical line
E126 (*^)    continuation line over-indented for hanging indent
E127 (^)    continuation line over-indented for visual indent
E128 (^)    continuation line under-indented for visual indent
E129 (^)    visually indented line with same indent as next logical line
E131 (^)    continuation line unaligned for hanging indent
E133 (*)    closing bracket is missing indentation
E2    Whitespace
E201    whitespace after ‘(‘
E202    whitespace before ‘)’
E203    whitespace before ‘:’
E211    whitespace before ‘(‘
E221    multiple spaces before operator
E222    multiple spaces after operator
E223    tab before operator
E224    tab after operator
E225    missing whitespace around operator
E226 (*)    missing whitespace around arithmetic operator
E227    missing whitespace around bitwise or shift operator
E228    missing whitespace around modulo operator
E231    missing whitespace after ‘,’, ‘;’, or ‘:’
E241 (*)    multiple spaces after ‘,’
E242 (*)    tab after ‘,’
E251    unexpected spaces around keyword / parameter equals
E261    at least two spaces before inline comment
E262    inline comment should start with ‘# ‘
E265    block comment should start with ‘# ‘
E266    too many leading ‘#’ for block comment
E271    multiple spaces after keyword
E272    multiple spaces before keyword
E273    tab after keyword
E274    tab before keyword
E275    missing whitespace after keyword
E3    Blank line
E301    expected 1 blank line, found 0
E302    expected 2 blank lines, found 0
E303    too many blank lines (3)
E304    blank lines found after function decorator
E305    expected 2 blank lines after end of function or class
E306    expected 1 blank line before a nested definition
E4    Import
E401    multiple imports on one line
E402    module level import not at top of file
E5    Line length
E501 (^)    line too long (82 > 79 characters)
E502    the backslash is redundant between brackets
E7    Statement
E701    multiple statements on one line (colon)
E702    multiple statements on one line (semicolon)
E703    statement ends with a semicolon
E704 (*)    multiple statements on one line (def)
E711 (^)    comparison to None should be ‘if cond is None:’
E712 (^)    comparison to True should be ‘if cond is True:’ or ‘if cond:’
E713    test for membership should be ‘not in’
E714    test for object identity should be ‘is not’
E721 (^)    do not compare types, use ‘isinstance()’
E722    do not use bare except, specify exception instead
E731    do not assign a lambda expression, use a def
E741    do not use variables named ‘l’, ‘O’, or ‘I’
E742    do not define classes named ‘l’, ‘O’, or ‘I’
E743    do not define functions named ‘l’, ‘O’, or ‘I’
E9    Runtime
E901    SyntaxError or IndentationError
E902    IOError
W1    Indentation warning
W191    indentation contains tabs
W2    Whitespace warning
W291    trailing whitespace
W292    no newline at end of file
W293    blank line contains whitespace
W3    Blank line warning
W391    blank line at end of file
W5    Line break warning
W503 (*)    line break before binary operator
W504 (*)    line break after binary operator
W505 (*^)    doc line too long (82 > 79 characters)
W6    Deprecation warning
W601    .has_key() is deprecated, use ‘in’
W602    deprecated form of raising exception
W603    ‘<>’ is deprecated, use ‘!=’
W604    backticks are deprecated, use ‘repr()’
W605    invalid escape sequence ‘x’
W606    ‘async’ and ‘await’ are reserved keywords starting with Python 3.7


 二:  PEP 8: E101 indentation contains mixed spaces and tabs 解决方案

如上图由于不规范的缩进,PyCharm工具自动识别到缩进包含多个空格或制表位,导致不符合 EP8 代码规范,警告信息: PEP 8: E101 indentation contains mixed spaces and tabs

通常有两种解决方式:

 2.1:  划线部分的代码,缩进一个tab制表位,或者两个制表位:

2.2: file-->setting-->检索框中输入PEP 8,然后去掉下图箭头所指的两个复选框即可---》确认即可

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1060079.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

为什么要用PLL时钟芯片替换传统晶体和振荡器?

随着社会智能化程度越来越高&#xff0c;数字化转型全面加速&#xff0c;市场对电子系统的精确度要求也越来越高&#xff01; 电子系统&#xff0c;如何保障运行精确度&#xff1f; 一般情况下需要用到“时钟信号”&#xff0c;用来同步各种组件的操作体系对应的数字逻辑&…

stm32之雨滴传感器使用记录

一、简介 雨滴传感器、烟雾传感器&#xff08;MQ2&#xff09;、轨迹传感器、干黄管等的原理都类似&#xff0c;都是将检测到的信号通过LM393进行处理之后再输出&#xff0c;可以输出数字信号DO&#xff08;0和1&#xff09;和模拟信号A0。 雨滴传感器在正常情况下是AO输出的是…

插入排序:简单而有效的排序方法

在计算机科学中&#xff0c;排序算法是一个重要且常见的主题&#xff0c;它们用于对数据进行有序排列。插入排序&#xff08;Insertion Sort&#xff09;是其中一个简单但有效的排序算法。本文将详细解释插入排序的原理和步骤&#xff0c;并提供Java语言的实现示例。 插入排序的…

react项目从webpack迁移到vite的解决方案

虽然webpack是前端工程编译工具的王者&#xff0c;但是最近vite牛逼吹的震天响&#xff0c;说什么开发/生产打包速度甩webpack 100条街。不管是不是事实&#xff0c;总得尝试一下吧。 于是说干就干&#xff0c;在网上找了很多资料&#xff0c;终于搞定了&#xff0c;以下就是r…

QT实现TCP服务器客户端

服务器 .cpp #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);//实例化一个服务器server new QTcpServer(this);// 此时&#xff0c;服务器已经成功进入监听状…

区间搜索指令(博途SCL)

S型速度曲线行车位置控制,停靠位置搜索功能会用到区间搜索指令,下面我们详细介绍区间搜索指令的相关应用。 S型加减速行车位置控制(支持点动和停车位置搜索)-CSDN博客S型加减速位置控制详细算法和应用场景介绍,请查看下面文章博客。本篇文章不再赘述,这里主要介绍点动动和…

sheng的学习笔记-【中文】【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第一周测验

课程1_第1周_测验题 目录&#xff1a;目录 第一题 1.“人工智能是新电力” 这个比喻指的是什么&#xff1f; A. 【  】人工智能为我们的家庭和办公室的个人设备供电&#xff0c;类似于电力。 B. 【  】通过“智能电网”&#xff0c;人工智能正在传递新一波的电力。 C. …

【多级缓存】

文章目录 1. JVM进程缓存2. Lua语法3. 实现多级缓存3.1 反向代理流程3.2 OpenResty快速入门 4. 查询Tomcat4.1 发送http请求的API4.2 封装http工具4.3 基于ID负载均衡4.4 流程小结 5. Redis缓存预热 传统的缓存策略一般是请求到达Tomcat后&#xff0c;先查询Redis&#xff0c;如…

App分发苹果ios内测ipa应用文件签名分发平台剖析其运行模式及法律注意事项

随着移动应用的快速发展&#xff0c;为了确保应用的质量和稳定性&#xff0c;开发者们通常在发布应用之前会进行内部测试。而App内测签名分发平台作为一种解决方案&#xff0c;不仅能够提供快速的应用分发和安装&#xff0c;还能确保应用的完整性和可靠性。本文将详细分析App内…

【算法|动态规划No.11】leetcode53. 最大子数组和

个人主页&#xff1a;兜里有颗棉花糖 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 兜里有颗棉花糖 原创 收录于专栏【手撕算法系列专栏】【LeetCode】 &#x1f354;本专栏旨在提高自己算法能力的同时&#xff0c;记录一下自己的学习过程&#xff0c;希望…

input允许多行输入

input允许多行输入 input允许多行输入 ______________________________ 表示停止输入想象一下&#xff0c;我们现在身处一间充满活力的课堂。学生们坐在自己的座位上&#xff0c;准备聆听老师的讲解。老师站在讲台上&#xff0c;充满激情地开始教授代码。 老师&#xff1a;同…

星球作业(第十期)Android中的ClassLoader

Android中的ClassLoader 1.Android中有哪几种ClassLoader&#xff1f;它们的作用和区别是什么&#xff1f; 2.简述ClassLoader的双亲委托模型 Android中有哪几种ClassLoader&#xff1f;它们的作用和区别是什么&#xff1f; Android中有三个ClassLoader&#xff0c;分别是Bas…

【智慧导诊系统源码】智慧导诊系统的技术支撑与实际运作

什么是智慧导诊系统? 简单地说&#xff0c;智慧导诊系统是一种利用人工智能技术&#xff0c;为医生提供帮助的系统。它可以通过分析患者的症状和病史为医生提供疾病诊断和治疗方案的建议。 智慧导诊系统需要具备以下技术支撑才能实现 人工智能技术支撑。智慧导诊系统的核心在…

设计模式9、组合模式 Composite

解释说明&#xff1a;组合多个对象形成树形结构以表示具有部分-整体关系的层次结构。组合模式让客户端可以统一对待单个对象和组合对象。 抽象根节点&#xff08;Component&#xff09;&#xff1a;定义系统各层次对象的共有方法和属性&#xff0c;可以预先定义一些默认行为和属…

两种新建CAA项目快捷启动方式

文章目录 一、前言二、新建项目快捷启动方式&#xff08;相当于直接Cnext进入&#xff09;方式一&#xff1a;按下面图示操作&#xff08;以V5 R21为例&#xff09;方式二&#xff1a;按下面图示操作&#xff08;以V5 R18为例&#xff09; 一、前言 环境变量配置文件可存放路径…

字典与数组第七讲:工作表数据计算时为什么要采用数组公式(一)

《VBA数组与字典方案》教程&#xff08;10144533&#xff09;是我推出的第三套教程&#xff0c;目前已经是第二版修订了。这套教程定位于中级&#xff0c;字典是VBA的精华&#xff0c;我要求学员必学。7.1.3.9教程和手册掌握后&#xff0c;可以解决大多数工作中遇到的实际问题。…

个人论坛系统的测试用例

首先要弄清楚论坛系统都具有什么功能&#xff0c;然后对各个功能进行分类编写测试用例。

使用Sen2cor对Sentinel-2辐射定标和大气校正

使用Sen2cor对Sentinel-2辐射定标和大气校正 在上一篇文章中&#xff0c;先对Sen2cor进行了安装 先定位到插件所在文件夹下 cd C:\Users\lenovo\AppData\Local\Sen2Cor-02.11.00-win64如果在上一篇中已经配置好环境变量&#xff0c;那就不用cd定位了&#xff0c;直接输后面的…

经典算法-----八皇后问题

目录 前言 八皇后问题 1.问题简介 1.2思路剖析 1.3递归和回溯 代码实现 ​编辑 1.递归回溯解决 能否放置数组 完整代码&#xff1a; 2.非递归回溯解决 前言 今天我们学习一个新的算法&#xff0c;也就是回溯算法&#xff0c;就以八皇后问题作为示例&#xff0c;这是…

剑指offer——JZ25 合并两个排序的链表 解题思路与具体代码【C++】

一、题目描述与要求 两个链表的第一个公共结点_牛客题霸_牛客网 (nowcoder.com) 题目描述 输入两个无环的单向链表&#xff0c;找出它们的第一个公共结点&#xff0c;如果没有公共节点则返回空。&#xff08;注意因为传入数据是链表&#xff0c;所以错误测试数据的提示是用其…