Detailed Explanation of Advanced Application of python Function

  • 2021-12-13 08:52:46
  • OfStack

Preface

Function is well known, and the definition format of function in python is as follows:

def function name (formal parameter):

Function body

Function encapsulation is to realize the reuse of code blocks. python has built-in 1 basic function. Developers can also define their own functions. Functions can only be defined first and then called.

1. Steps for a function call

1. When the program encounters a function call, it pauses execution

2. Pass the actual parameter value to the function parameter

3. Execute the function body statement

4. Return a value and continue down

2. Packaging and unpackaging of parameters

Packing

Parameter packaging means that multiple parameters can be processed at the same time, which is also called variable-length parameters. Variable-length parameters mainly have two forms when defining functions * parameter and **parameter. The former is used to accept any number of parameters and put them in a tuple, while the latter receives multiple key parameters to form key-value pairs and puts them in a dictionary.

Package as a tuple

def demo(*p):
  print(p)
demo(1,2,3,4,5,6)
# Results 
(1,2,3,4,5,6)
Package as a dictionary

def demo(**p):
  print(p)
demo(x=1,y=2,z=3)
# Results 
{'x':1,'y':2,'z':3}

# Parameter packaging is to set the formal parameter to the form of * parameter name, and the actual parameter is multiple parameters, which is an iterative object that will process multiple parameters as tuples or dictionaries, and package individuals as a whole.

Unpacking

Corresponding to the packing of parameters, the unpacking of parameters also has two forms: s and **s. When calling functions with multiple positional parameters, iterative objects such as lists, tuples and sets can be used as arguments, and one is added before the argument name. Python will automatically unpack them and pass the values in the sequence to multiple formal parameters respectively.


def demo(a,b,c,d):
  print(a+B*c/d)
list1=[1,2,3,4]
demo(*list1)
# Results 
2.5

If the argument is a dictionary, it can be unpacked in the form of * * parameter name, which will convert the dictionary into a form similar to key parameters for parameter passing. For this form of sequence unpacking, it is required that all keys in the argument dictionary must be the names of function parameters or correspond to variable-length parameters of two types in the function.


s={'a':1,'b':2,'c':3}
def demo(a,b,c=4)
  print(a,b,c)
demo(**s)
# Results 
1 2 3

3. Scope of variables

The scope of a variable is the space in which the variable can act

Variables are divided according to scope: global variables and local variables

Global variables: Variables defined outside the function are global variables

Global variables can be used both inside and outside a function, but cannot be modified directly inside a function. If you modify a global variable inside a function, you must add the global keyword (python does not recommend modifying the value of a global variable inside a function)

Local variables: Variables defined within a function are local variables

Local variables are defined within a function, but outside a function local variables are invalid

Local variables defined inside a function and global variables defined outside a function are two concepts (variables have the same name)


a=1
def sum()
  a=3
# Outside the function here a And within the function a Are two variables  

4. Document comments and variable names for variables

1. Function doc document

The function doc document is used to describe the function function and is a special comment within the function


def sum(a,b):
  '''
   Returns the sum of two numbers 
  '''
  return a+b

2. Obtaining the function name

__name__ method


>>> print(print.__name__)
print
>>>

3. Obtaining the document of function doc

__doc__ method

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: