A brief discussion on Python's function knowledge

A brief discussion on Python's function knowledge

Two major categories of function parameters

Formal parameters are the parameters written in parentheses during the function definition phase. Actual parameters are the parameters passed in parentheses during the function call phase.
The relationship between formal parameters and actual parameters Formal parameters can be regarded as variable names, and actual parameters can be regarded as variable values. They are temporarily bound during the function call phase and disconnected when the function is finished. There are many forms of variable names and actual parameters (grasp the core data value)

insert image description here

Positional parameters

Positional parameters are the positional parameters filled in from left to right. Variable name positional parameters are filled in from left to right during the function definition phase. Data values ​​are filled in from left to right during the function call phase.
Keyword arguments (can break positional order)
	During the function call phase, the value is passed in the form of parameter name = data value. 1. Positional parameters and positional actual parameters are bound according to the corresponding position during the function call phase. 2. When binding positional parameters, one more or one less is not allowed. Key points: The simpler the format, the earlier it is, and the more complex it is, the later it is.	

Variable length parameters

1. The function can run normally no matter how many positional parameters are passed in. Variable length parameters def func(x,y,*a):
     print(x,y,a)
 func() # ()
 func(1) # (1,)
 func(1, 2, 3, 4, 5, 6, 7) # (1, 2, 3, 4, 5, 6, 7)
 func(1,2) # 1 2 ()
 func(1,2,3,4,5,6,7,8,9) # 1 2 (3, 4, 5, 6, 7, 8, 9)
 func(1,2) # 1 2 (3, 4, 5, 6, 7, 8, 9)
* is used in the parameter to receive extra positional parameters and organize them into tuples and assign them to the variable name after *
2. The function can run normally no matter how many keywords are passed in def index(x, y, **b):
     print(x, y, b)
 index() # {}
 index(a=1,b=2,c=3,d=4) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
 index(y=2, x=1) # 1 2 {}
 index(y=2, x=1, u=222, k=111, l=444) # 1 2 {'u': 222, 'k': 111, 'l': 444}
**In the parameter, it is used to receive extra keyword parameters and organize them into a dictionary to assign to the variable name behind it. *Using it will break up the data in the list and tuple. **Using it will break up the key-value pairs of the dictionary into keyword parameters and pass them in.

Namespace

	1. Built-in namespace print()
        	len()
	2. Global namespace Code written in the top grid of the py file name = 'jason' # name global def func(): # func global pass
            if 1:
                a = 123 # a global for i in range(10):
                print(i) # i global while True:
                a = 123 # a global 3. Local namespace After the function body code is run, the local namespace is generated. 

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Python variables, data types, data type conversion related functions usage examples
  • Python method to check function parameter data type through decorator
  • Summary of commonly used python data type conversion functions
  • Python basics variables and data types
  • Python beginner definition function
  • Python's six basic data types and common functions are shown

<<:  User experience of portal website redesign

>>:  Docker installs ClickHouse and initializes data testing

Recommend

How to use node to implement static file caching

Table of contents cache Cache location classifica...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

The difference between method=post/get in Form

Form provides two ways of data transmission - get ...

MySQL query tree structure method

Table of contents MySQL query tree structure 1. A...

Vue Basic Tutorial: Conditional Rendering and List Rendering

Table of contents Preface 1.1 Function 1.2 How to...

JavaScript implements H5 gold coin function (example code)

Today I made a Spring Festival gold coin red enve...

The whole process of configuring reverse proxy locally through nginx

Preface Nginx is a lightweight HTTP server that u...

Understanding the CSS transform-origin property

Preface I recently made a fireworks animation, wh...

Common array operations in JavaScript

Table of contents 1. concat() 2. join() 3. push()...

Implementation example of react project from new creation to deployment

Start a new project This article mainly records t...

Detailed explanation of bash command usage

On Linux, bash is adopted as the standard, which ...

Using NTP for Time Synchronization in Ubuntu

NTP is a TCP/IP protocol for synchronizing time o...

Pagination Examples and Good Practices

<br />Structure and hierarchy reduce complex...

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...