Python regulation

 We all know that the Python code standards that programmers use today is:

PEP 8 -- Style Guide for Python Code | Python.org

I based on the standard, according to their own habits, to adapt, now list the following code habits, easy to understand.

Coding

• Files are encoded in UTF-8 unless otherwise specified

• Unless otherwise specified, the position of the Python translator should be added to the header of the file

• The code takes into account the efficiency of other Python implementations, such as the '+' operator, which is very efficient in CPython (Python) and very inefficient in Jython, so the.join() method should be used.

• Use 'is' and' is not 'instead of' == 'whenever possible, e.g. if x is not None is better than if x.

• With class-based exceptions, each module or package has its own Exception class that inherits from Exception.

• Do not use naked except, except followed by specific exceptions.Try as little code as possible in an exception.

Code Format

  • 1-The indentation

             Use 4 Spaces for indentation, do not use Tap, and do not mix Tap and Spaces.

  • 2-line width

             Each line of code should be limited to 80 characters, with backslashes and parentheses preferred for line breaks.A newline point is followed by a return type.In special cases can be slightly more than 80, but the longest shall not exceed 120, a screen can be read, do not need to move around.

  • 3-The blank line

             Two empty lines between top-level definitions, such as function or class definitions

             Method definitions, class definitions and the first method should all have a blank line

             Multiple blank lines can be used to separate groups of related functions

             Functions can use blank lines to separate logically related code

             Try to avoid blank lines elsewhere.

  • 4-The blank space

             Avoid unnecessary Spaces.

             Empty one space on both sides of binary operator [=,-,+=,==,>,in,is not, and]:

             No Spaces before closing parentheses, commas, colons, and semicolons, and no Spaces before opening parentheses of functions.Such as Func (1).

             Do not place a space before the opening parenthesis of a sequence.If the list [2].Do not add Spaces for alignment.

              Function default arguments use assignment operators that omit Spaces.

  • 5-The newline

             The second line is indented to the beginning of the parentheses.

1.	foo = long_function_name(var_one, var_two,
2.	                         var_three, var_four)

             Indent the second line with 4 Spaces. This works when the opening parenthesis is a line break.

1.	def long_function_name(
2.	        var_one, var_two, var_three,
3.	        var_four):
4.	    print(var_one)

             With a backslash \ newline, the binary operator +. Etc should appear at the end of the line;Long strings can also be wrapped this way.

1.	session.query(MyTable).\
2.	        filter_by(id=1).\
3.	        one()
4.	 
5.	print 'Hello, '\
6.	      '%s %s!' %\
7.	      ('Harry', 'Potter')
  • 6-Quotes

             Natural language uses double quotation marks "...", such as error messages

             Machine identifiers use single quotes '...', such as the key in dict

             Regular expressions use native double quotes r"..."

             DocString (docString) uses three double quotation marks """......"""

  • 7-Comment

             When a piece of code changes, the first thing you do is change comments!Comments must be in English, preferably in full sentences, with a capital letter and a terminator followed by two Spaces to begin the next sentence.If it is a phrase, you can omit the terminator.

             1. Block comment, a comment added before a piece of code.Add a space after the '#'.Paragraphs are separated by only '#' lines.

             2. Lines of comment, followed by a comment.X = x + 1 # Increment X

But use it sparingly.

              3. Avoid unnecessary comments.

Naming conventions

        As a general rule, new code should be named in the following style, and existing library code should be as styled as possible.

  1. Use the lowercase letter 'L', uppercase letter 'O' and other confusing letters.
  2. Module name as short as possible, use all lowercase way, can use the underscore.
  3. The package name should be short. Use all lowercase letters instead of underscores.
  4. The name of class uses CapWords, big camel name, all the first letters of words are capitalized, and no special characters, underscores and numbers are used. The class used in the module uses _CapWords.
  5. Name exceptions using CapWords+Error suffixes.
  6. Global variables are valid only in modules, similar to static in C.There are two implementation methods, one is __all__ mechanism;The second is prefixed with an underscore.Do not use global variables without special requirements
  7. The function name is all lowercase. You can use underscores, but the underscore cannot start with a letter
  8. Constant names are all uppercase, can be underlined, start with a uppercase letter, all seen in the project Settings file
  9. The attributes (methods and variables) of class are named in all lowercase and can be underlined.
  10. Class attributes have three scopes: public, non-public, and subclass API. They can be understood as public, private, and protected in C++. Non-public attributes are preceded by an underscore.
  11. If the attribute of class conflicts with the name of the keyword, suffix 1 underline, try not to use abbreviations and other ways.
  12. To avoid naming conflicts with subclass attributes, prefix some class attributes with two underscores.For example, the Foo class declares __a. Only foo. _Foo__a can be accessed to avoid ambiguity.If the subclass is also called Foo, nothing can be done.
  13. Class methods must take self as their first argument, while static methods must take CLS as their first argument.The first argument to the static method must be CLS.

The ginseng

  1. Do not use keywords for variables and parameters
  2. Do not have a list of default values in the method parameter default values (parameters pass Pointers).
  3. The method returns True or False first, followed by data, but be sure to return the same type of data, not if return True, else return data.
  4. It's nerve-racking.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值