Home > Backend Development > Python Tutorial > What's the Difference Between `*args` and `kwargs` in Python Function Parameters?

What's the Difference Between `*args` and `kwargs` in Python Function Parameters?

Barbara Streisand
Release: 2024-12-29 03:18:18
Original
135 people have browsed it

What's the Difference Between `*args` and `kwargs` in Python Function Parameters?

Unveiling the Significance of (Double Star) and (Star) in Function Parameters*

In Python, function parameters denoted by args and *kwargs serve as versatile mechanisms to accommodate arbitrary arguments.

Unpacking Positional Arguments with *args

The *args parameter gathers all positional arguments that surpass the predefined ones into a tuple. For instance:

def foo(*args):
    for arg in args:
        print(arg)
Copy after login

This function can accept an arbitrary number of positional arguments, such as:

foo(1)  # Output: 1
foo(1, 2, 3)  # Output: 1 2 3
Copy after login

Assembling Keyword Arguments with kwargs**

On the other hand, **kwargs collects all keyword arguments into a dictionary.

def bar(**kwargs):
    for key, value in kwargs.items():
        print(key, value)
Copy after login

Calling this function with keyword arguments yields:

bar(name='John', age=30)  # Output: name John, age 30
Copy after login

Interplay of args and kwargs*

Both idioms can be combined to allow a mix of fixed and variable arguments:

def foo(kind, *args, bar=None, **kwargs):
    print(kind, args, bar, kwargs)
Copy after login

This function can be called as follows:

foo(123, 'a', 'b', apple='red')  # Output: 123 ('a', 'b') None {'apple': 'red'}
Copy after login

Additional Use Cases

  • Unpacking Argument Lists: The * idiom can be used to unpack argument lists when calling a function:
def foo(bar, lee):
    print(bar, lee)

baz = [1, 2]
foo(*baz)  # Output: 1 2
Copy after login
  • Extended Iterable Unpacking (Python 3 ): * can be used on the left side of an assignment to obtain a list:
first, *rest = [1, 2, 3, 4]
# first = 1
# rest = [2, 3, 4]
Copy after login
  • Keyword-Only Arguments (Python 3 ): Functions can restrict keyword arguments by using the following syntax:
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass
Copy after login

This function requires three positional arguments and any number of keyword arguments after *.

The above is the detailed content of What's the Difference Between `*args` and `kwargs` in Python Function Parameters?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template