Use of python functions and modules
In python, using functions can reduce code repeatability and increase program modularity. The purpose of using functions is to reduce programming difficulty and code reuse.
In Python, you can use the def keyword to define functions. Like variables, each function also has a loud name, and the naming rules are consistent with those of variables. The parameters passed to the function can be placed in parentheses behind the function name, which is very similar to the function in mathematics. The parameters of the function in the program are equivalent to the independent variables of the function in mathematics. After the function is executed, we can return a value through the return keyword, which is equivalent to the dependent variables of the function in mathematics.
Parameters of function
Function is a "building block" of code supported by most programming languages, but there are still many differences between functions in python and functions in other languages, one of which is the processing of function parameters in python. In python, function parameters can have default values or use variable parameters, so python does not need to support function overloading like other languages, because we can use a function in many different ways when defining it. The following are two small examples.
from random import randint def roll_dice(n=2): """Chromophore""" total = 0 for _ in range(n): total += randint(1, 6) return total def add(a=0, b=0, c=0): """Add three numbers""" return a + b + c # If no parameters are specified, shake two dice with the default value print(roll_dice()) # Shake three dice print(roll_dice(3)) print(add()) print(add(1)) print(add(1, 2)) print(add(1, 2, 3)) # Parameters can be passed in different order print(add(c=50, a=100, b=200))
We have set default values for the parameters of the above two functions, which means that if the value of the corresponding parameter is not passed in when calling the function, the default value of the parameter will be used. Therefore, in the above code, we can call the add function in various ways, which is consistent with the effect of function overloading in many other languages.
In fact, the above add function has a better implementation scheme, because we may add 0 or more parameters, and the specific number of parameters is determined by the caller. As the designer of the function, we know nothing about this. Therefore, when the number of parameters is uncertain, we can use variable parameters. The code is as follows
# A * in front of the parameter name indicates that args is a variable parameter def add(*args): total = 0 for val in args: total += val return total # 0 or more parameters can be passed in when calling the add function print(add()) print(add(1)) print(add(1, 2)) print(add(1, 2, 3)) print(add(1, 3, 5, 7, 9))
Manage functions with modules
For any programming language, naming identifiers such as variables and functions is a headache, because we will encounter the embarrassing situation of naming conflict. The simplest scenario is to define two functions with the same name in the same. py file. Since python does not have the concept of function overloading, the following definitions will overwrite the previous definitions, which means that only one of the two functions with the same name exists.
def foo(): print('hello, world!') def foo(): print('goodbye, world!') # What will the following code output? foo()
Of course, we can easily avoid the above situation, but if the project is developed by multiple people in a team, multiple programmers in the team may have defined a function called foo, so how to solve this naming conflict? In fact, the answer is very simple. Each file in python represents a module. We can have functions with the same name in different modules. When using the function, we can import the specified module through the import keyword to distinguish which foo function in the module to use. The code is as follows.
def foo(): print('hello, world!')
def foo(): print('goodbye, world!')
from module1 import foo # Output hello, world! foo() from module2 import foo # Output goodbye, world! foo()
You can also distinguish which foo function to use in the following way.
import module1 as m1 import module2 as m2 m1.foo() m2.foo()
But if the code is written in the following way, the program calls the foo that was imported last, because the foo after import covers the foo that was imported before.
from module1 import foo from module2 import foo # Output goodbye, world! foo()
from module2 import foo from module1 import foo # Output hello, world! foo()
It should be noted that if the module we import has executable code in addition to defining functions, the Python interpreter will execute these codes when importing the module. In fact, we may not want this. Therefore, if we write execution code in the module, it is best to put these execution codes into the conditions shown below, In this case, unless the module is run directly, these codes under the if condition will not be executed, because only the name of the directly executed module is "_main_".
def foo(): pass def bar(): pass # __ name__ Is an implicit variable in Python, which represents the name of the module # Only the name of the module directly executed by the Python interpreter is__ main__ if __name__ == '__main__': print('call foo()') foo() print('call bar()') bar()
import module3 # When importing module3, the code when the if condition in the module is true will not be executed, because the name of the module is module3 instead of__ main__