Detailed explanation of the principle of Python function parameter classification

1. Definition of parameters

1、 Where are the parameters of the function defined

When defining a function in python, the parentheses after the function name are used to define the parameters. If there are multiple parameters, the parameters are directly separated by commas.

case:

# Using the parameters of the function, define a function that can add any two numbers
def add_num(a,b):
 c = a + b
 print(c)

**2、 Function call with parameters: **

The function defines the parameters, so you need to pass in the parameters when calling the function

add_num(11,22)

operation result

33

In the above case, the parameters defined in parentheses after the function name when we define the function are called formal parameters.

When we call a function, the parameters passed in are called actual parameters, and formal parameters are used to receive actual parameters.

Two, parameter classification

Above we talked about formal parameters and actual parameters. When defining and calling formal parameters and actual parameters, they can be divided into several categories according to different forms.

1、 Classification according to actual parameters

Actual parameters: positional parameters, keyword parameters

1.1、 Positional parameters (unnamed parameters)

def func(a,b,c):print(a)print(b)print(c)add_num(11,22,33)
# operation result
112233

In the above case, the three formal parameters of the function are to receive the actual parameters passed in by position. We call this form of passing parameters as positional parameters.

1.2、 Keyword parameters (named parameters)

def func(a,b,c):print(a)print(b)print(c)add_num(11,c=99,b=33)
# operation result
113399

When calling a function, the actual parameter is specified by the parameter name and passed to a certain formal parameter. This form of passing parameters, we call it a keyword parameter

Note: When passing parameters, write positional parameters first, and then write named parameters

2、 Classification according to formal parameters

Formal parameters can be divided into three categories: mandatory parameters, silent parameters, and variable length parameters

2.1、 Required parameters:

Parameters that must be passed when calling the function

def add(a,b):
 c=a+b
 print(c)add(11,22)

The a and b in the above function are necessary parameters, which must be passed when calling the function, otherwise an error will be reported

2.2、 Default parameters (default parameters):

When calling a function, you can pass it or not, or use the default value if you don’t pass it

def func(a,b,c=99):print(a)print(b)print(c)
​
func(11,22,33)print('-----------')func(55,66)
​
# operation result:
112233---------------556699

From the above cases, we can find that when we call for the first time, we pass in three parameters, c prints out the value we passed in, the second time only passes in two parameters, this time c prints What comes out is the default value we set when we defined

Note: The parameters with default values must be at the end of the parameter list.

2.3、 Variable length parameters *args and **kwargs

You can pass 0 or more when calling a function

2.3.1、* args: Receive multiple incoming positional parameters and save them in the form of an ancestor

def func(*args):  print(args)func(33,44,55,66,77)func(*(33,44,55,66,77))#operation result(33,44,55,66,77)(33,44,55,66,77)

You can pass in directly when calling: func(33,44,55,66,77),

You can also assemble the list or tuple first, and then pass in through unpacking: func((33,44,55,66,77));

2.3.2、**kwargs: Receive multiple incoming keyword parameters and save them in the form of a dictionary

def func(**kwargs):print(kwargs)func(e=33,h=44,f=55,d=66,c=77)func(**{'e':33,'h':44,'d':66,'c':77})
# operation result
{' e':33,'h':44,'f':55,'d':66,'c':77}{'e':33,'h':44,'f':55,'d':66,'c':77}

**kwargs, kw receives a dictionary;

Keyword parameters can be directly passed in: func(11,22,e=33,h=44,f=55,d=66,c=77),

You can also assemble the dict first, and then pass it through unpacking: func({'e':33,'h':44,'d':66,'c':77}).

Note: Using *args and **kwargs is Python's idiom. Of course, other parameter names can also be used, but idioms are better.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python super() method
Detailed explanation of the principle of Python timer thread pool
Detailed explanation of -u parameter of python command
Detailed explanation of the usage of Python decimal module
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of the attribute access process of Python objects
Detailed explanation of the remaining problem based on python (%)
Detailed explanation of python backtracking template
Detailed explanation of python sequence types
Python function definition and parameter explanation
Detailed explanation of Python IO port multiplexing
What is the id function of python
python3 realizes the function of mask drawing
What is the function body of python
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the use of pip in Python | summary of third-party library installation
What is the function of adb in python
Detailed explanation of python standard library OS module
Detailed explanation of how python supports concurrent methods
Detailed explanation of data types based on Python
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Python method of parameter passing
Consolidate the foundation of Python (3)
Detailed implementation of Python plug-in mechanism
How Python implements the mail function
Detailed explanation of ubuntu using gpg2
Python handles the 4 wheels of Chinese
Python error handling assert detailed explanation
Python simulation of the landlord deal
What is the use of Python
Detailed explanation of the installation and use of SSH in the Ubuntu environment
Detailed usage of dictionary in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Detailed analysis of Python garbage collection mechanism
Two days of learning the basics of Python
Implementation principle of dynamic binding of Python classes
Python from attribute to property detailed explanation
Where is the pip path of python3
The essence of Python language: Itertools library