Article directory
Detailed Function Parameters
1. Default parameters
When calling the function, if the value of the default parameter is not passed in, the default value will be taken.
The following example will print the default age, if age is not passed in:
def printinfo(name, age=35):
# 打印任何传入的字符串
print("name: %s" % name)
print("age %d" % age)
# 调用printinfo函数
printinfo(name="miki") # 在函数执行过程中 age取默认值35
printinfo(age=9 ,name="miki")
The output of the above example:
name: miki
age: 35
name: miki
age: 9
Summary:
Parameters with values by default in formal parameters are called default parameters
Note: Parameters with default values must be at the end of the parameter list
def printinfo(name, age=35, sex):
print name
File "<stdin>", line 1
SyntaxError: non-default argument follows default argument
2. Indefinite length parameter
Sometimes it may be necessary for a function to handle more parameters than originally declared. These parameters are called variable-length parameters and will not be named when declared.
The basic syntax is as follows:
def functionname([formal_args,] *args, **kwargs):
"""函数_文档字符串"""
function_suite
return [expression]
Notice:
- The variable args with an asterisk (*) will store all unnamed variable parameters, args is a tuple
- The variable kwargs added with ** will store named parameters, that is, parameters in the form of key=value, and kwargs is a dictionary.
def test(a, b, *args, **kwargs):
"函数在声明时,需要两个参数"
print('a={},b={},args={},kwargs={}'.format(a,b,args,kwargs))
test(2, 3, '你好', 'hi', 'how do you do', name="zhangsan", age=18)
# a=2,b=3,args=('你好', 'hi', 'how do you do'),kwargs={'name': 'zhangsan', 'age': 18}
b = ('hi', '大家好', '今天天气真好')
d = {
'name': "zhangsan", "age": 19}
# 注意,在传入参数时的星号问题。
test(10, 20, *b, **d)
# a=10,b=20,args=('hi', '大家好', '今天天气真好'),kwargs={'name': 'zhangsan', 'age': 19}
# 如果在传值时,不使用星号,会把后面的参数当做 args
test(10,20,b,d)
# a=10,b=20,args=(('hi', '大家好', '今天天气真好'), {'name': 'zhangsan', 'age': 19}),kwargs={}
3. The default parameters are behind *args
def sum_nums_3(a, *args, b=22, c=33, **kwargs):
print(a)
print(b)
print(c)
print(args)
print(kwargs)
sum_nums_3(100, 200, 300, 400, 500, 600, 700, b=1, c=2, mm=800, nn=900)
Note:
If many values are variable-length parameters, then in this case, the default parameters can be placed after args, but if there are **kwargs, kwargs must be the last
mutable and immutable types
Summarize
-
The so-called variable type and immutable type refer to: data can be directly modified, if it can be directly modified, it is variable, otherwise it is immutable
-
Variable types (modify data, memory address will not change) are: list, dictionary, collection
-
Immutable types (modify data, memory address must change) are: numbers, strings, tuples
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model