Python coding standards

7. What is PEP8?

8号Python增强提案,是针对Python代码格式而编写的风格指南

8. The Zen of Python understand it?

通过 import this 语句可以获取其具体的内容。它告诉大家何写出高效整洁的代码

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

9. Learn DocStrings it?

DocStrings 文档字符串是一个重要工具,用于解释文档程序,帮助你的程序文档更加简单易懂。主要是解释代码作用的。

10. Learn what types of annotations?

PEP 484 引入了类型提示,这使得可以对 Python 代码进行静态类型检查。 在使用 Ide 的时候可以获取到参数的类型,更方便传入参数。使用格式如下

def foo(num: int) -> None:
    print(f"接收到的数字是:{num}")

15. Python code indentation in whether to support the Tab key and spaces mix.

不允许 tab 键和空格键混用,这种现象在使用 sublime 的时候尤为明显。一般推荐使用 4 个空格替代 tab 键。

16. Can you import multiple libraries in an import in?

可以是可以,但是不推荐。因为一次导入多个模块可读性不是很好,所以一行导入一个模块会比较好。同样的尽量少用 from modulename import *,因为判断某个函数或者属性的来源有些困难,不方便调试,可读性也降低了。

17. What should be noted in the file name to Py time?

给文件命名的时候不要和标准库库的一些模块重复,比如 abc。 另外要名字要有意义,不建议数字开头或者中文命名。

To name a few standardized tools Python style

pylint 和 flake8

Guess you like

Origin www.cnblogs.com/D-M-C/p/11099005.html