Several common modes of Python functions

Discover the small usage of pycharm

This calling method is called positional arguments, and parameters are passed in in the order of calling

This is a more general demonstration of the above example

Keyword argument call method

This is called an arbitrary argument. If you use this when you don't know how many parameters the user will pass in the future when calling the function, it will be assembled into a tuple object and passed to this deformable parameter.

To call this function, you must pass in two parameters

Join us now, such a requirement is to put these two numbers in a tuple or list?

error

This time it's ok, but it's numb. It's more troublesome

If you add a * as an actual parameter and pass it to the called function, python will automatically parse the parameter from it to the function

This method is called: unseal the argument list

● The parameter of a function can have a default value, which is called a default parameter. The parameter with an * before the parameter name is called a deformable parameter, and the parameter with two ** before the parameter name is called a dictionary parameter. The deformable parameter must be after the non-default parameter, the default parameter must be after the non-default parameter and the deformable parameter, and the dictionary parameter must be placed last.

● The formal parameters in the function definition are in order. The actual parameters can be passed to the formal parameters according to the position, which is called the positional actual parameter, or the actual parameter can be passed to the formal parameter in the form of formal parameter name=actual parameter, which is called the key Word argument. The keyword arguments can be arranged in any order.

● You can pass multiple actual parameters to the deformable parameter, and these actual parameters are packed into a tuple object and passed to the deformable parameter. The function can access the actual parameters in the deformable parameter like a normal tuple object.

● It is possible to pass the dictionary parameters to the dictionary parameters in a key-value manner. These key-value arguments are packaged into a dictionary object and passed to the dictionary parameters. The function can access each key-value argument in the dictionary parameter like a normal dictionary object.

● If the actual parameters to be passed to the function are placed in a tuple or list object, they can be passed to the called function by adding * in front of the variable name that points to the object. The parameter will be unblocked and passed to the formal parameter of the called function. If the actual parameters to be passed to the function are placed in a dict object, you can pass the dictionary actual parameters to the formal parameters by adding ** in front of the variable name that points to the object.

Recommended Posts

Several common modes of Python functions
09. Common modules of Python3
Collection of Python Common Modules
07. Python3 functions
There are several versions of python
Python functions
Summary of common operations of Python time module
Analysis of common methods of Python multi-process programming
Functions in python
7 features of Python3.9
Analysis of common methods of Python operation Jira library
Detailed explanation of common tools for Python process control
Basics of Python syntax
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
Consolidate the foundation of Python(6)
Python common data structure collation
Analysis of JS of Python crawler
2020--Python grammar common knowledge points
python king of glory wallpaper
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Python version of OpenCV installation
python functions, classes, modules, packages
Python common exception handling mechanism
9 feature engineering techniques of Python
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)