Summary of Points for Attention in Writing Functions in python

  • 2021-10-16 02:06:14
  • OfStack

1. Attention to writing

(1) Assign a description name to the function.

(2) Function names include only lowercase letters and underscores.

(3) Each function should contain comments that briefly describe its functionality, immediately following the function definition and in document string format.

2. When you specify default values for formal parameters, there must be no spaces on both sides of the equal sign


def function_name(parameter_0, parameter_1='default value')

3. If a program or module contains multiple functions, you can use two empty lines to separate adjacent functions, so that it is easier to know where the first function ends and where the next function starts.


function_name(value_0, parameter_1='value')

For keyword arguments in function calls, you should also follow that all import sentences should be placed at the beginning of the file. The only exception to 1 is to use comments at the beginning of the file to describe the whole program.

Extension of knowledge points:

1. The first step in code reuse is to use functions.

2.1 Functions can accept any number (including 0) of any type of value as input variables and return any number (including 0) of any type of result.

3. The 1 cuts in 3. Python are all objects, including strings, numbers, ancestors, lists, dictionaries, and functions.

4. Calling Python function help () can print out the parameter list and specification document of one function. If you just want to get the string of the document, you can use doc.

5. You can take functions as arguments to functions, and functions can also be elements of lists, tuples, collections, and dictionaries. The function name is immutable, so you can use the function as the key of the dictionary. Python's functional level 1 object, that is to say, we can directly refer to functions and pass them to other functions as parameters, and compare and judge them through expressions and if statements.

6. A closure is a function that can be dynamically generated by another function, and can change and store the values of variables created outside the function. (It can also be said that a closure is a function defined in a scope that refers to a variable in that scope.)

7. Python uses special rules to compare two tuples. It first compares the corresponding elements with subscript 0 in each tuple. If it is equal, compare the corresponding element with subscript 1. If it is still equal, continue to compare the element with subscript 2, and so on.

8. When a variable is referenced in an expression, the Python interpreter traverses the scopes in the following order:

9. When you assign values to variables, the rules are different. If this variable is already defined in the current scope, it will have a new value. If the variable does not exist in the current scope, Python will treat the assignment as a definition of the variable, and the scope of the newly defined variable is the function containing the assignment operation.

10. nonlocal statement. nonlocal makes it clear that if you assign a value to the variable in the closure, you are modifying the variable in the scope outside the closure. However, the only limitation of nonlocal is that it cannot extend to the module level, which is to prevent it from polluting the global scope. This complements the global statement. global is used to denote an assignment to the variable that directly modifies the variable in the scope.

11. Consider using a generator to overwrite a function that returns a list directly

12. Be careful when iterating parameters


Related articles: