Several Python Function Method Design Principles summarized
This article mainly introduces several Python function method design principles, this article explains how to do one thing for each function, keep it simple, keep it short, input and use parameters, and output and use return statements. For more information, see
In any programming language, function applications are mainly due to the following two situations:
1. Repeated code blocks. At this time, you must consider using functions to reduce program redundancy.
2. The code block is complex. functions can be used to enhance the readability of the program.
When the process is complex enough, consider functions and how to combine them. In Python, function design mainly takes into account three aspects: function size, aggregation, and coupling. These three aspects should be attributed to the scope of planning and design. High Cohesion and low coupling are the general principles of function design in any language.
1. How to break down tasks into more targeted functions, resulting in aggregation
2. How to Design inter-function communication involves Coupling
3. How to Design the function size to enhance its aggregation and reduce its coupling
[Aggregation]
Each function only does one thing.
Perfect program design, every function should and only need to do one thing.
For example, put an elephant in the refrigerator in three steps: Open the door, put the elephant in, and close the door.
In this way, you should write three functions instead of one function to complete all the tasks. In this way, the structure is clear and the layers are clear and easy to understand!
[Size]
Keep simple and keep short
Python is a process-oriented language and an object-oriented language, but it mostly acts as a scripting language.
For the same function, Python is used to implement the code length. It may be 1/3. A few hundred lines of code in C/C ++/Java and other languages!
If a function designed in the project needs to be viewed on pages, you should consider splitting the function.
In Python's more than 200 modules, it is rare to see that a function has two or three pages.
Python code is known for its simplicity and clarity. A function that is too long or has deep nesting often becomes a sign of design defects.
[Coupling]
Input parameters and output return statements
This allows the function to be independent from its external stuff. Parameters and return statements are the best way to isolate external dependencies.
Use global variables with caution
The first consideration: global variables are usually a bad way to communicate between functions.
It will lead to dependency and timing problems, which will lead to difficulties in program debugging and modification.
Second consideration: local variables are far faster than global variables in terms of code and performance optimization.
Search for variables in Python in sequence: Local function variable => upper-layer function variable => global variable => built-in variable
It can be seen from the above that the local variable is preferentially searched. Once found, it stops. The test results are as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
Import profile A = 5 Def param_test (): B = 5 Res = 0 For I in range (100000000 ): Res = B + I Return res If _ name __= = '_ main __': Profile. run ('Param _ test ()') >>>================================================== RESTART = >>> 5 function cballs in 37.012 seconds # global variable test result: 37 seconds Ordered by: standard name Ncalltottime percall cumtime percall filename: lineno (function) 1 19.586 19.586 19.586 19.586: 0 (range) 1 1.358 1.358 1.358: 0 (setprofile) 1 0.004 0.004 35.448 35.448 <string >:1 (<module>) 1 15.857 15.857 35.443 35.443 Learn. py: 5 (param_test) 1 0.206 0.206 37.012 37.012 profile: 0 (param_test ()) 0 0.000 0.000 profile: 0 (profiler) >>>================================================== RESTART = >>> 5 function CILS in 11.504 seconds # local variable test result: 11 seconds Ordered by: standard name Ncalltottime percall cumtime percall filename: lineno (function) 1 3.135 3.135 3.135 3.135: 0 (range) 1 0.006 0.006 0.006: 0 (setprofile) 1 0.000 0.000 11.497 11.497 <string >:1 (<module>) 1 8.362 8.362 11.497 11.497 Learn. py: 5 (param_test) 1 0.000 0.000 11.504 11.504 profile: 0 (param_test ()) 0 0.000 0.000 profile: 0 (profiler) |
Avoid changing variable type parameters
Python data types such as list and dictionary are variable objects. When passed as a parameter to a function, it is sometimes modified like a global variable.
The disadvantage of doing so is that the coupling between functions is enhanced, resulting in the function being too special and unfriendly. It is also difficult to maintain.
In this case, we should consider using the S [:] Slice and the copy () function and the deepcopy () function in the copy module to make a copy to avoid modifying the variable object.
For details, refer to this article: how to copy Python
Avoid directly changing variables in another module
For example, in B. in The py file, import module a with the variable PI = 3.14, But B. py wants to change it to PI = 3.14159. Here you cannot figure out the original value of the variable PI. In this case, you can consider using an easy-to-understand Function Name:
?
1 2 3 4 5 6 |
# Module a. py PI = 1, 3.14 Def setPi (new ): PI = new Return PI |
In this way, both the desired PI value and the PI value in module a are not changed.
?
1 2 3 4 |
Import PI = a. setPi (3.14159) Print PI; a. PI |