In the course of learning the Python coding norms, naming conventions are important knowledge points. We need to have a full understanding of this knowledge. Here we will give a systematic explanation of the Python naming conventions.
- Parse the correct operation method when Python calls the zip command
- Explanation of common examples of Python ure Module
- Explanation of Python plug-in PyDev's correct configuration method
- Sample Code for calling Python functions in C
- Analysis of Python programming specifications
Python naming convention Module name
The name of the module should be short and lowercase. Because the module name is mapped to the file name, some file systems are case insensitive and the long name is truncated. It is important to select a short module name, which is not a problem on Unix, but it may be a problem when the code is uploaded to Mac or Windows.
When an extension module written in C or C ++ has a high-level (for example, object-oriented) interface that is accompanied by a Python module, the C/C ++ module has an underscore (for example: _ socket ). The Python package should be a short, all lowercase name without any underscores.
Python naming conventions
Almost unexpectedly, the class name uses the CapWords convention. The internal class is added with a leading underline.
Exception name in Python naming conventions
If the module defines a single exception for all situations, it is usually called "error" or "Error ". It seems that the built-in (extended) module uses "error" (for example, OS. error), while the Python module usually uses "Error" (for example, xdrlib. Error ). The trend seems to be the tendency to use CapWords exception names.
Global variable name in Python naming conventions
(Let's pray that these variables only make sense within one module)
These conventions are the same as those in functions. The module is designed to be used through "from M import *". It must use an underscore as the prefix of global variables (and internal functions and classes) to prevent it from being exported (exporting ).
Function Name in Python naming conventions
The function name should be in lower case. It may use underlined words to increase readability. MixedCase is only allowed for context (such as threading. py) That is dominant in this style to maintain backward compatibility.
Method Name and instance variable
This section is basically the same as the function: usually use lower-case words. If necessary, separate them with underscores to increase readability. Only use a leading underline for internal methods and instances that do not intend to serve as the public interface of the class. Python does not force this requirement: It depends on whether the programmer complies with this Convention.
Use two leading underlines to indicate private class names. Python connects these names and class names together:
If the class Foo has an attribute named _ a, it cannot be accessed with Foo. _. (Stubborn users can still get access through Foo. _ Foo _ .)
The double-leading underline is used only to avoid name conflicts between attribute names in classes containing subclasses.