current position:Home>Parameter passing of Python function
Parameter passing of Python function
2022-01-30 19:12:30 【Blind brother Python】
Positional arguments
Positional parameters are the most common python How to pass the parameters of a function . According to the order defined by the parameters , Just use it in sequence .
def f(a, b, c, d):
print(" Parameters a:", a)
print(" Parameters b:", b)
print(" Parameters c:", c)
print(" Parameters d:", d)
# According to the location , Pass parameters
# The first corresponds to the first , The last one corresponds to the last one
f("a", "b", "c", "d")
Copy code
Default parameters
sometimes , We hope that some parameters can not be passed , It should have a default value , If we pass this parameter in the function call , Then use the parameters we passed , If the function is called , This parameter was not passed , Just use the default values .
def f(a, b, c="c", d="d"):
print(" Parameters a:", a)
print(" Parameters b:", b)
print(" Parameters c:", c)
print(" Parameters d:", d)
# These three situations are the same
f("a", "b")
f("a", "b", "c")
f("a", "b", "c", "d")
# Use the parameters we passed
f("a1", "b1", "c1", "d1")
Copy code
Be careful : After default parameters , You can no longer define location parameters . Because if the location parameter appears after the default parameter , There will be no way to know if the default parameter was passed
Variable parameters
Sometimes we don't know how many parameters we need , At this time, we can set it as a variable parameter . adopt * Set the variable name , Variable parameters can accept multiple position parameters , And form them into a tuple, using .
def f(*a):
print(a)
f("a", "b") # The result is ('a', 'b')
f("a", "b", "c") # The result is ('a', 'b', 'c')
f("a", "b", "c", "d") # The result is ('a', 'b', 'c', 'd')
Copy code
Be careful : When defining functions , Position parameter... Cannot appear after variable parameter , The following variable parameters are regarded as keyword parameters .
Key parameters
Variable parameter the parameter passed in has no name , therefore , If we need a parameter with a name , Can pass ** Variable names accept keyword parameters . Keyword parameters can be through key=value In the form of , Final , Will automatically form a dictionary .
def f(**a):
print(a)
f(a=1, b=2, c=3) # The result is {'a': 1, 'b': 2, 'c': 3}
Copy code
Parameter qualification
Common position parameters , Both can be used as position parameters , It can also be used as a keyword parameter . therefore ,python Parameter limiting method is added ,/ The previous parameter must be a positional parameter ,* The following parameters must be keyword parameters .
def f(a, /, b, c, *, d, e):
print(a)
print(b)
print(c)
print(d)
print(e)
# This is not allowed , because d and e Must be a keyword parameter
# f("a", "b", "c", "d", "e")
# It is also not allowed , because a Must be a positional parameter
# f(a="a", b="b", c="c", d="d", e="e")
# The following methods are OK , And the effect is the same
f("a", "b", "c", d="d", e="e")
f("a", "b", c="c", d="d", e="e")
f("a", b="b", c="c", d="d", e="e")
# in addition , Because the keyword parameters are in no order , It's fine too
f("a", e="e", b="b", d="d", c="c")
Copy code
summary
1, Positional parameters cannot be placed after default parameters , Cannot be placed after keyword parameters .
2, Ordinary parameters without restrictions can be used as location parameters or keyword parameters at the same time .
3, Use variable parameters * Variable name , Use unlimited number of keyword parameters ** Variable name .
4,/ The previous parameter must be a positional parameter ,/ To * The parameters between are both positional parameters , It is also a keyword parameter ,* Subsequent parameters must be keyword parameters .
5, Location parameters must be passed by location , Keyword parameters do not need to be passed by location , But you need to point out the name .
copyright notice
author[Blind brother Python],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301912267659.html
The sidebar is recommended
- Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
- Turn audio into shareable video with Python and ffmpeg
- Using rbind in python (equivalent to R)
- Pandas: how to create an empty data frame with column names
- Talk about quantifying investment using Python
- Python, image restoration in opencv - CV2 inpaint
- Python notes (14): advanced technologies such as object-oriented programming
- Python notes (13): operations such as object-oriented programming
- Python notes (12): inheritance such as object-oriented programming
- Chapter 2: Fundamentals of python-5 Boolean
guess what you like
-
Python notes (11): encapsulation such as object-oriented programming
-
Python notes (10): concepts such as object-oriented programming
-
Gradient lifting method and its implementation in Python
-
Van * Python | simple crawling of a site course
-
Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
-
Nanny tutorial! Build VIM into an IDE (Python)
-
Fourier transform of Python OpenCV image processing, lesson 52
-
Introduction to python (III) network request and analysis
-
China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
-
Introduction to python (IV) dynamic web page analysis and capture
Random recommended
- Python practice - capture 58 rental information and store it in MySQL database
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
- This is what you should do to quickly create a list in Python
- On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
- Python OpenCV image area contour mark, which can be used to frame various small notes
- How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
- Initial Python tuple
- Introduction to Python urllib module
- Advanced Python Basics: from functions to advanced magic methods
- Python Foundation: data structure summary
- Python Basics: from variables to exception handling
- Python notes (22): time module and calendar module
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators