Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python Code style and PEP8

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you a sample analysis of Python code style and PEP8. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

The Python code style and the PEP8Python code style are described by PEP8. This document describes all aspects of the Python programming style. Subject to this document, Python code written by different programmers can maintain a maximum degree of similarity. This makes it easy to read and communicate between programmers.

1 variable

Constant: uppercase and underlined

USER_CONSTANT

For global variables that will not change, use uppercase and underlining.

Private variables: lowercase and a leading underscore

_ private_value

There are no private variables in Python. If you encounter variables that need to be protected, use lowercase and a leading underscore. But this is just a convention between programmers to warn that this is a private variable and that external classes should not access it. In fact, however, this variable can still be accessed by external classes.

Built-in variables: lowercase, two leading underscores and two rear underscores

_ _ class__

Two leading underscores cause the variable to be renamed during interpretation. This is to avoid conflicts between built-in variables and other variables. User-defined variables should strictly avoid this style. So as not to cause chaos.

2 functions and methods

In general, lowercase and underscore should be used. But some older libraries use mixed case, that is, the first word is lowercase, followed by the first letter of each word in uppercase and the rest in lowercase. But now, lowercase and underlining have become the norm.

Proprietary methods: lowercase and a leading underscore

Def _ secrete (self):

Print "don't test me."

Here, like private variables, it is not really private access. It should also be noted that general functions do not use two leading underscores (the name adaptation feature of Python will come into play when two leading underscores are encountered). It will be mentioned later in the special function.

Special methods: lowercase and two leading underscores, two rear underscores

Def _ _ add__ (self, other):

Return int. _ _ add__ (other)

This style applies only to special functions, such as operator overloading.

Function arguments: lowercase and underscore, with no spaces on both sides of the default equal sign

Def connect (self, user = None):

Self._user = user

Three categories

Classes are always named in hump format, that is, all words are capitalized and the rest are lowercase. The class name should be concise, precise, and sufficient to understand what the class has done. A common method is to use a suffix that represents its type or property, for example:

SQLEngine

MimeTypes

For the base class, you can use a Base or Abstract prefix

BaseCookie

AbstractGroup

Class UserProfile (object):

Def _ _ init__ (self, profile):

Return self._profile = profile

Def profile (self):

Return self._profile

4 modules and packages

With the exception of the special module _ _ init__, module names use lowercase letters without underscores.

If they implement a protocol, they usually use the suffix lib, for example:

Import smtplib

Import os

Import sys

5 about parameters

5.1 do not use assertions for static type checking

Assertions can be used to check parameters, but not just for static type checking. Python is a dynamically typed language, and static type checking violates its design idea. Assertions should be used to prevent functions from being called meaninglessly.

5.2 do not abuse * args and * * kwargs

The * args and * * kwargs parameters may destroy the robustness of the function. They blur the signature, and the code often starts to build small parameter parsers in the wrong places.

6 others

6.1 use has or is prefixes to name Boolean elements

Is_connect = True

Has_member = False

6.2 naming sequences in plural form

Members = ['user_1', 'user_2']

6.3 name the dictionary with an explicit name

Person_address = {'user_1':'10 road WD', 'user_2':'20 street huafu'}

6.4 avoid common names

Names such as list, dict, sequence or element should be avoided.

6.5 avoid existing names

Names that already exist in systems such as os and sys should be avoided.

7 some numbers

Number of columns per row: PEP 8 specifies 79 columns, which is a bit harsh. According to your own situation, for example, do not exceed the number of columns displayed by the editor when the screen is full. In this way, you can easily view the code without moving the horizontal cursor.

One function: no more than 30 lines of code can be displayed in a screen class, and you can see the entire function without using a vertical cursor.

One class: no more than 200 lines of code, no more than 10 methods.

A module should not exceed 500 lines.

8 validate script

You can install a pep8 script to verify that your code style conforms to PEP8.

> > easy_install pep8

> > pep8-r-- ignoire E501 Test.py

What this command line means is to type an error repeatedly and ignore the 501 error (more than 79 lines of code).

The above is the example analysis of Python code style and PEP8 shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report