Python code is more concise how good the training is not good Zhengzhou Python

  How Python code more concise? Zhengzhou Python training, OK? Many novice Python's junior partner in the new to Python code will ignore many of the details, such as variable naming, comments and other normative content. In practice, a good coding habits will help you be more efficient completion of work. Here we give share some learning Python code should be simple to master skills.

13183109-b7b4aac111eefdf7.jpg

  First, the naming

  Variables and functions should use lowercase letters naming functions and variables, and words separated by underscores, improve code readability.

  Declare variables should consider the use of non-built Python method name in the code, if you use the built-in Python method names use one or two underscores (). If you do not want to access the variable outside the class, you should use an underscore (_) as a prefix internal variables of the class. If you want to define a private variable names are keywords such as Python dict will use (__).

  Function declaration must be connected by a single underscore and lowercase letters, of course, to declare variables and functions privatization is similar. Beginning of the function using a single underscore, which was privatized. For the function uses keywords to Pyton in naming To use double underline, the additional use of clear and understandable variable names and important.

  Function parameters and methods to be followed with the naming of variables and method names the same rules. Because the self class method as the first key parameter. So do not use self as a keyword in a function as a parameter, so as to avoid confusion. Constant naming should usually defined constant name in uppercase letters.

  Second, the attention to detail expressions and statements

  1) Try to return the same type of statement. If you want the function to return a value, make sure that all execution paths of the function returns the value. However, if the desired function simply perform operations without return value, the return None Python implicitly as the default function. Therefore, we recommend learning Python's junior partner must pay attention to, write readable code, write code that does not matter, readability is very important.

  2) Use isinstance () method instead of type () for comparison. When comparing two object types, to use the isinstance () instead of type, since the isinstance () determines whether an object is a subclass of another object is true. Consider a scenario: if the data structure is passed dict subclass, such orderdict. type () for a particular type of data structure will fail; however, isinstance () which can recognize that it is a subclass of dict.

  3) use the document string. Python is recommended to use the official language, "" triple double quotes "" to write documentation string, even if the string line with his party will use triple quotes. When you want to extend this comment useful. Documenting the string on multiple lines of code is a method of recording more descriptive way, you can use Python multi-line documentation string descriptive document written in Python code string, instead of writing on each row comments.

  Of course on Python in the specification and many in this small series will not list them. To learn Python technology, the proposed professional learning better.

Reproduced in: https: //www.jianshu.com/p/750fe8e02426

Guess you like

Origin blog.csdn.net/weixin_33893473/article/details/91228573