Like C/C++, Java and other languages, python also has a set of agreed-upon rules for naming. Naming in accordance with the specifications can make the program much more readable, thus making the code more logical and easy for yourself and other collaborators to understand the meaning of the code in future expansions, thus improving the efficiency of writing code.
We need to pay attention to the following points when writing programs in general:
I. Hard rules for naming python variables
1.1 Case-sensitive variable names
Python variable names are case-sensitive, which means that Student and student represent two different names in the python language.
1.2. python's variable names can contain English, underscores, and numbers, but they cannot begin with a number.
That is, student_id, student1, student_1, student, _student, _, etc. are all reasonable naming, but 1student is illegal naming.
II. Variables named in different styles represent different types
Unlike the naming convention in java, which uses the hump nomenclature (it is possible that the big brother of the programmer who invented java not only likes to drink Java's coffee, but also likes to ride the camels on Java, haha), python variable naming generally adopts the serpentine nomenclature (after all, the original meaning of python is boa constrictor), which means that if the variable name is made up of two words, then an underscore is used to connect these two words, such as student_id. of course, we will also see similar hump nomenclature variable names in python, for professional programmers, these naming are not just randomly written, they conform to certain standards, which are summarized below:
2.1. Module (module) naming style
Modules are named using lowercase as much as possible, initial letters are kept in lowercase, and try not to use underscores (except in the case of multiple words, and not too many of them)
# Correct module name import decoder import html_parser # Unrecommended module names import Decoder
2.2. Class naming
Class names use the CamelCase naming style, with the first letter capitalized, and private classes may begin with an underscore.
class Farm(): pass class AnimalFarm(Farm): pass class _PrivateFarm(Farm): pass
2.3. Function (function) naming
Function names are always lowercase, and if there is more than one word, they are separated by underscores.
def run(): pass def run_with_env(): pass
2.4. Variable naming
Lowercase variable names as much as possible, if there is more than one word, separate them with an underscore.
if __name__ == '__main__': count = 0 school_name = ''
2.5. Naming Constants
If we want to use a symbol to represent a constant (a quantity whose value is unchanging, such as the speed of light, π, etc.), use all capitalization, and if there is more than one word, use underscores to separate them.
MAX_CLIENT = 100 MAX_CONNECTION = 1000 CONNECTION_TIMEOUT = 600
III. Special naming starting with "underscore"
Often times, we see variables in python source code that begin with an underscore, and beginners feel weird seeing these variables. Indeed, these "_" names have a special meaning:
3.1. Naming beginning with a single underscore
- If a class variable starts with a single underscore "_", it means that the variable cannot be accessed directly, similar to the protected type in C++, and such a variable cannot be imported.
- module_name import.
- Methods that begin with one underline indicate that the method is not part of the AP and should not be accessed directly (although there is nothing wrong with accessing it syntactically).
3.2. Naming beginning with a double underscore
- Class variables that begin with two underlines are private members of the class and cannot be accessed by imports or other class variables.
- For methods in a class, using a double underscore at the beginning of the header indicates that the subclass cannot override the method. Don't use this unless you really know what you're doing.
3.3. Naming that begins with a double underscore and ends with a double underscore
Magic methods: Variables that begin and end with a double underscore are Python specific and have a special identity. We generally refer to such methods as "magic methods".
- Magic methods are python built-in methods that don't need to be called actively, and exist for the purpose of being called by the python interpreter. Almost every magic method has a corresponding built-in function, or operator, and when we use these functions or operators on the object, we will call the corresponding magic method in the class, which can be interpreted as an override of these python built-in functions.
- You can define this class method when you want your defined objects to use some built-in functions or operators (such as len, add, +,, ==, etc.) just like Python's built-in objects.
- Of course, there are also some properties that only have an underscore at the end. This is just to avoid conflicting names with Python's reserved keywords, and has no special meaning.
- The use of underlined variables (methods) is often related to the design of classes in object-oriented programming, for more in-depth knowledge, seeThis blog.. Of course, if one needs to know more about the python programming specification, theOfficial documentation for PEP8 (Python Enhancement Proposal8)It is the best reference.
to this article about a text to easily master the python language naming convention rules of the article is introduced to this, more related python naming convention content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!