Home  >  Article  >  Backend Development  >  Understand the role of "*" in Python in one minute

Understand the role of "*" in Python in one minute

烟雨青岚
烟雨青岚forward
2020-06-24 11:50:575569browse

Understand the role of

One minute to understand the role of "*" in Python

Used when passing parameters and function definitions to functions When specifying parameters, you will often see the symbols * and **. Their functions are explained below.

Use * and ** when calling functions
Assume there is a function
def test(a, b, c)

test(* args): The function of * is actually to pass each element in the sequence args as a positional parameter. For example, in the above code, if args is equal to (1,2,3), then this code is equivalent to test(1, 2, 3).

test(**kwargs): The function of ** is to pass the dictionary kwargs into keyword parameters. For example, in the above code, if kwargs is equal to {‘a’:1,’b’:2,’c’:3}, then this code is equivalent to test(a=1,b=2,c=3).

Use * and ** when defining function parameters

def test(*args):
The meaning of * when defining function parameters It's different again. *args here means that all the positional parameters passed in are stored in the tuple args. For example, if the function above calls test(1, 2, 3), the value of args will be (1, 2, 3). :

def test(**kwargs):
Similarly, ** is for keyword parameters and dictionaries. If test(a=1,b=2,c=3) is called, the value of kwargs is {‘a’:1,’b’:2,’c’:3}.

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/yhs_cy/article/details/79438706

Recommended tutorial: "python tutorial"

The above is the detailed content of Understand the role of "*" in Python in one minute. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete