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 SessionStorage and LocalStorage in Javascript

Table of contents Preface Introduction to Session...

Common JavaScript memory errors and solutions

Table of contents 1. Timer monitoring 2. Event mo...

A simple way to implement Vue's drag screenshot function

Drag the mouse to take a screenshot of the page (...

Goodbye Docker: How to Transform to Containerd in 5 Minutes

Docker is a very popular container technology. Th...

How to use Docker Swarm to build WordPress

cause I once set up WordPress on Vultr, but for w...

Use of Linux date command

1. Command Introduction The date command is used ...

How to set a fixed IP in Linux (tested and effective)

First, open the virtual machine Open xshell5 to c...

In-depth reading and practice records of conditional types in TypeScript

Table of contents Using conditional types in gene...

How to enable or disable SSH for a specific user or user group in Linux

Due to your company standards, you may only allow...

How to find the specified content of a large file in Linux

Think big and small, then redirect. Sometimes Lin...