Module 03 · [Optional] Python Coding Practices and Packaging Concepts(可选)Python 编码实践与打包概念
Module Summary: Python Coding Practices and Packaging Concepts模块总结:Python 编码实践与打包概念
Congratulations! You have completed this module. At this point, you know that:
恭喜!您已完成本模块。至此,您已了解:
- The PEP8 guidelines for code readability include the following:
PEP8 代码可读性指南包括以下内容:- Four spaces for indentation
缩进使用四个空格 - Blank lines to separate functions and classes
使用空行分隔函数和类 - Spaces around operators and after commas
运算符周围和逗号后的空格
- Four spaces for indentation
- The PEP8 coding conventions for consistency and manageability include:
PEP8 编码规范旨在保持一致性和可管理性,其中包括:- Add larger blocks of code inside functions
将较大的代码块添加到函数内部 - Name functions and files using lowercase with underscores
使用小写字母和下划线命名函数和文件 - Name classes using CamelCase
使用驼峰命名法命名类 - Name constants in capital letters with underscores separating words
使用大写字母和下划线命名常量
- Add larger blocks of code inside functions
- To ensure that your code adheres to the predefined style and standard without executing the code, you can use the Static code analysis method.
为了确保您的代码在不执行的情况下符合预定义的样式和标准,您可以使用静态代码分析方法。 - Unit testing is a method to validate if code units are operating as designed. You must test every unit before integration with the final codebase.
单元测试是一种验证代码单元是否按设计运行的方法。在与最终代码库集成之前,您必须测试每个单元。 - To create a package: 创建包的步骤:
- Create a folder with the package name
创建与包同名的文件夹 - Create an empty
__init__.pyfile
创建一个空的__init__.py文件 - Create the required modules
创建所需的模块 - In the
__init__.pyfile, add code to reference the modules needed in the package
在__init__.py文件中,添加代码以引用包中所需的模块
- Create a folder with the package name
- You can verify the package via the bash terminal in a Python shell.
您可以通过 Python shell 中的 bash 终端验证该包。