6.1 functions
When implementing large projects, the code that needs to be reused is often extracted and defined as a function. Thus, the programming workload is simplified and the program structure is simplified
6.1. 1 define function
-
def statements are used to define functions
def Function name(Parameter table): Function statement return Return value The return value and parameters can be omitted
6.1. 2 call function
-
Basic format of calling function
Function name(Parameter table)
-
example
# Define function def add(a, b): return a + b # Call function t = add(10, 20) print(t) # The output is 30
6.1. 3 parameters of function
The parameters in the parameter table of the function definition are called formal parameters, which are called formal parameters for short. When calling a function, the parameters provided in the parameter table are called actual parameters, which are referred to as actual parameters for short. Arguments can be constants, expressions, or variables. When the argument is a constant or expression, the calculation result of the constant or expression is directly passed to the formal parameter.
-
Parameter polymorphism: different results can be obtained when the types of arguments passed by the same function are different
# Define function def add(a, b): return a + b add(1,2) # Perform digital addition add('abc','def') # Perform string connection add((1,2),(3,4)) # Perform tuple merge add([1,2],[3,4]) # Perform list merge
-
Parameter assignment and passing: Python allows you to pass an argument to a specified formal parameter in the way of formal parameter assignment
def add(a, b): return a + b add(a='ab',b='cd') # Pass the parameter by assignment, and the result is' abcd ' add(b='ab',a='cd') # Pass the parameter by assignment and the result is' cdab '
-
Pass reference of variable object: when the argument references a variable object, if the formal parameter is modified in the function, the argument also obtains the modified object
- In python, strings, tuples, and numbers are immutable objects, while list,dict, and so on are modifiable objects
def f(a): a[0] = 'abc' x = [1,2] f(x) # Call the function to pass a reference to the list object print(x) #The output result is ['abc',2]
-
Indefinite length parameter
- Add a * in front of the formal parameter, which becomes an indefinite length tuple formal parameter and can receive all position arguments. The type is tuple
- Add two * * before the formal parameter. The formal parameter becomes an indefinite length dictionary formal parameter, which can receive all keyword real graphs. The type is dictionary
6.1. 4 function nesting definition
- Python allows you to define functions inside functions, that is, internal functions
- Internal functions can only be used inside functions
6.1.5 lambda function
-
lambda functions, also known as expression functions, are used to define anonymous functions
-
lambda is just an expression, and the function body is much simpler than def
-
The body of a lambda is an expression, not a code block. Only limited logic can be encapsulated in lambda expressions
-
lambda functions have their own namespace and cannot access parameters outside their own parameter list or in the global namespace
-
Although lambda functions can only write one line, they are not equivalent to C or C + + inline functions. The purpose of the latter is to call small functions without occupying stack memory, so as to increase operation efficiency
-
Basic format
lambda Parameter table:expression
-
example
add = lambda a,b:a+b add(1,2) # The result is 3
6.1. 6 recursive function
-
Recursive function refers to calling the function itself in the function body
def fac(n): if n==0: #Termination condition of recursive call return 1 else: return n*fac(n-1) # Call the function itself recursively
6.1. 7 function list
-
Because a function is an object, you can use it as a list element and then call the function through the list index
d = [lambda a,b:a+b,lambda a,b:a*b] d[0](1,3) # Call the first function and the result is 4 d[1](1,3) # Call the second function and the result is 3
6.2 scope of variable
The scope of a variable is the usable range of the variable, also known as the namespace of the variable. When assigning a value to a variable for the first time, Python creates a variable, and the location of the created variable determines the scope of the variable
- Scope classification
- Local scope: when there is no internal function, the function body is used as the local scope. The variables and function parameters created by assignment in the function belong to the local scope
- Function nested scope: when an internal function is included, the function body is a function nested scope
- global scope
- File scope: the internal of program files (also known as module files) is the file scope
- Built in scope: the environment of Python runtime is built-in scope, which contains various predefined variables and functions of Python
6.3 modules
A module is a program file that contains variables, functions, or classes. Modules can also contain various other Python statements
6.3. 1 import module
-
Import statement: the import statement is used to import the whole module. You can use as to specify a new name for the imported module
#Method 1 import module name #Use: module name Function name import my_ module1 print(my_ module1. num) # Use my_ Variable num in module1 my_ _module1. func() # Call my_ func function in module1 dog = my_ _module1.Dog() #Call my__ Create objects from classes in module1 dpg. show_ _info( )
-
From statement: the from statement is used to import the specified objects in the module. The imported objects can be used directly
#Method 2: from module name, import function name 1, function name 2, #Usage: function name #Note: if there is a ten thousand Dharma name with the same name, it will be covered ) from my_ _module2 import func, num from my_ _module1 import num func() print( num) #Method 3 from Module name import * #Import all functions in the module #Usage: function name from my_ module2 import * print( num) func( ) dog = Dog() dog. show_ _info() #as alias You can alias modules and functions #Note: if you use the as alias, you can no longer use the original name import my_ module1 as mm1 from my_ module1 import func as m1_ func from my_ _module2 import func as m2_ func mm1. func() m1_ _func() m2_ func()
6.4 package
Modules with similar or similar functions are placed in a directory, and one is defined in the directory__ init__.py file, this directory is the package