In Python, *args
and **kwargs
are powerful tools for creating flexible functions that allow functions to accept a variable number of arguments. This is especially useful when you don't know in advance how many arguments will be passed to the function.
*args
? *args
allows a function to accept any number of positional arguments. These parameters are stored in a tuple.
<code class="language-python">def print_numbers(*args): for number in args: print(number) print_numbers(1, 2, 3, 4, 5)</code>
Output:
<code>1 2 3 4 5</code>
Here you can pass any number of numbers and they will all be printed. If no arguments are passed, args
will just be an empty tuple.
**kwargs
? **kwargs
allows a function to accept any number of keyword arguments. These parameters are stored in a dictionary, where the keys are the parameter names and the values are their corresponding values.
<code class="language-python">def print_info(**kwargs): for key, value in kwargs.items(): print(f"{key}: {value}") print_info(name="Alice", age=25, city="New York")</code>
Output:
<code>name: Alice age: 25 city: New York</code>
Here you can pass any number of key-value pairs. If no keyword arguments are passed, kwargs
will just be an empty dictionary.
*args
and **kwargs
You can use both *args
and **kwargs
in the same function. This allows you to handle both positional and keyword arguments.
<code class="language-python">def describe_person(*args, **kwargs): print("Attributes:") for arg in args: print(f"- {arg}") print("\nDetails:") for key, value in kwargs.items(): print(f"{key}: {value}") describe_person("Friendly", "Helpful", name="Bob", age=30, city="Boston")</code>
Output:
<code>Attributes: - Friendly - Helpful Details: name: Bob age: 30 city: Boston</code>
Here, *args
collects positional parameters (such as "Friendly" and "Helpful"), and **kwargs
collects keyword parameters (such as name="Bob" and age=30).
*args
. **kwargs
. *args
**kwargs
<code class="language-python">def add_numbers(*args): total = sum(args) print(f"The sum is: {total}") add_numbers(1, 2, 3, 4) add_numbers(10, 20)</code>
Output:
<code>The sum is: 10 The sum is: 30</code>
<code class="language-python">def greet_people(**kwargs): for name, greeting in kwargs.items(): print(f"{greeting}, {name}!") greet_people(Alice="Hello", Bob="Hi", Charlie="Hey")</code>
Output:
<code>Hello, Alice! Hi, Bob! Hey, Charlie!</code>
*args
and **kwargs
<code class="language-python">def shopping_list(*items, **prices): print("Items to buy:") for item in items: print(f"- {item}") print("\nPrices:") for item, price in prices.items(): print(f"{item}: ${price}") shopping_list("Apples", "Bananas", Apples=2, Bananas=1.5, Oranges=3)</code>
Output:
<code>Items to buy: - Apples - Bananas Prices: Apples: Bananas: .5 Oranges: </code>
By using *args
and **kwargs
you can make your Python functions more dynamic and flexible. This is especially useful when working with programs where the number of inputs may vary. Try these features out starting with small projects and you'll find them very handy!
The above is the detailed content of Understanding *args and **kwargs in Python. For more information, please follow other related articles on the PHP Chinese website!