python编程入门到精通(大亭碎碎念六)

python编程入门到精通(大亭碎碎念六)(1)

python编程入门到精通(大亭碎碎念六)(2)

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是Learning Yard学苑。

今天大亭为大家带来我的“python编程规范”,

欢迎您的访问!

Share interest,spread happiness ,

increase knowledge and leave beautiful.

Dear you,this is the Learing Yard Academy!

Today, Dating will brings you “The Programming specifications of Python”,Welcome your visit!

一、缩进

缩进并不只是为了美观,也不仅仅为了增加可读性。每个缩进级别应采用 4 个空格。连续行所包装的元素应该要么采用 Python 隐式排列,即垂直对齐于圆括号、方括号和花括号,要么采用悬挂缩进,而在采用悬挂缩进时需考虑:第一行不应该包括参数,并且在续行中需要再缩进一级以便清楚表示,悬挂缩进可以不采用4空格的缩进方法。

Indentation is not only for aesthetic purposes, but also for readability. Each indent level should have 4 spaces. The elements packed by consecutive lines should either be arranged implicitly in Python, that is, vertically aligned with parentheses, square brackets and curly brackets, or be indented by hanging. When adopting hanging indent, it is necessary to consider that the first line should not include parameters, and the continued lines need to be indented one more level for clear indication. Hanging indent can not adopt the indent method of four spaces.

多行结束右圆/方/花括号可以单独一行书写,和上一行的缩进对齐,也可以和多行开始的第一行的第一个字符对齐,但不可以和上一行最后一个字符对齐。推荐的换行方式是利用 Python 圆括号、方括号和花括号中的隐式续行。很长的行可以通过在括号内换行来分成多行,最好加上反斜杠来区别续行。

The right circle/square/curly bracket at the end of multiple lines can be written on a single line, aligned with the indent of the previous line, or aligned with the first character of the first line at the beginning of multiple lines, but not with the last character of the previous line. The recommended line breaking method is to use the implicit line continuation in Python parentheses, square brackets, and curly brackets. Long lines can be divided into multiple lines by wrapping them in parentheses. It is better to add a backslash to distinguish continuation lines.

二、导入函数

导入应该写在代码文件开头,位于模块注释和文档字符串之后,模块全局变量和常量声明之前。应该按照下面的顺序分组来写:标准库imports-相关第三方imports-本地应用/库的特定imports,不同组的导入之间用空格隔开。常见有三种导入方法:第一种是import模块名,使用这种方法导入,如果模块名很长可以使用[as别名];第二种是from模块名import对象名,同样的在使用时可以给模块名起别名;第三种是from 模块名 import *,但这种方法简单粗暴,一下子导入所有内容,会降低可读性,如果多个模块中有同名对象,之前导入的同名对象全部无法访问,仅最后导入的模块中的对象有效,不利于代码的后期维护。

The import should be written at the beginning of the code file, after the module comments and document strings, and before the module global variables and constants declaration. It should be written in the following order: standard library imports - related third-party imports - specific imports of local applications/libraries. The imports of different groups should be separated by spaces. There are three common import methods: the first is to import the module name. If the module name is very long, you can use [as alias]; The second is from module name import object name, which can also be aliased when used; The third method is from module name import *. However, this method is simple and crude. Importing all content at once will reduce readability. If there are objects with the same name in multiple modules, all previously imported objects with the same name cannot be accessed. Only the objects in the last imported module are valid, which is unfavorable for later code maintenance.

今天的分享就到这里了,

如果您对文章有独特的想法,欢迎给我们留言。

让我们相约明天,祝您今天过得开心快乐!

That's it for today's sharing. If you have a unique idea about the article.please leave us a message.

Let us meet tomorrow I wish you a happy day today!

参考资料:百度

翻译来源:百度翻译

声明:本文由LearningYard新学苑原创,如有侵权,请联系删除!

END

文案|雷雅婷

排版|雷雅婷

审核|闫庆红

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页