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

烟雨青岚
Release: 2020-06-24 13:33:52
forward
5613 people have browsed it

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

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!