avatarRoushanak Rahmat, PhD

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1677

Abstract

d'</span>) <span class="hljs-comment"># Prints: Hello World</span></pre></div><h1 id="5342">kwargs (Keyword Arguments):</h1><ul><li>The <code>kwargs</code> parameter allows a function to accept any number of keyword arguments, which are typically specified as <b><i>key-value pairs</i></b>.</li><li>The <code></code> before <code>kwargs</code> tells Python to unpack the keyword arguments passed to the function and store them in a dictionary named <code>kwargs</code>.</li><li>Similar to <code>*args</code>, you can choose any name for the parameter, but the <code></code> symbol is required.</li><li>Example:</li></ul><div id="bade"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">my_function</span>(<span class="hljs-params">**kwargs</span>): <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> kwargs.items(): <span class="hljs-built_in">print</span>(key, value)

my_function(name=<span class="hljs-string">'Alice'</span>, age=<span class="hljs-number">25</span>) <span class="hljs-comment"># Prints: name Alice, age 25</span> my_function(city=<span class="hljs-string">'New York'</span>, country=<span class="hljs-string">'USA'</span>) <span class="hljs-comment"># Prints: city New York, country USA</span></pre></div><h1 id="ecb1">*args and **kwargs together in a function definition:</h1><p id="a75b">It’s worth noting that you can use <code>*args</code> and <code>**kwargs</code> together in a function definition. In such cases, <code>*args</code> captures non-keyword arguments, while <code>**kwargs</code> captures keyword arguments.</p><div id="e382"><pre><span class="hljs-keyw

Options

ord">def</span> <span class="hljs-title function_">print_arguments</span>(<span class="hljs-params">*args, **kwargs</span>): <span class="hljs-keyword">for</span> arg <span class="hljs-keyword">in</span> args: <span class="hljs-built_in">print</span>(<span class="hljs-string">"Positional argument:"</span>, arg)

<span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> kwargs.items():
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"Keyword argument -"</span>, key + <span class="hljs-string">":"</span>, value)

print_arguments(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, name=<span class="hljs-string">'Alice'</span>, age=<span class="hljs-number">25</span>)</pre></div><p id="9bb6">In this example, the <code>print_arguments</code> function accepts a variable number of positional arguments using <code>*args</code> and a variable number of keyword arguments using <code>**kwargs</code>. It then iterates over the positional arguments and prints each one, and also iterates over the keyword arguments and prints the key-value pairs.</p><div id="d508"><pre><span class="hljs-attr">Positional argument:</span> <span class="hljs-number">1</span> <span class="hljs-attr">Positional argument:</span> <span class="hljs-number">2</span> <span class="hljs-attr">Positional argument:</span> <span class="hljs-number">3</span> <span class="hljs-attr">Keyword argument - name:</span> <span class="hljs-string">Alice</span> <span class="hljs-attr">Keyword argument - age:</span> <span class="hljs-number">25</span></pre></div></article></body>

*args vs **kwargs

Photo by Hitesh Choudhary on Unsplash

In Python programming, *args and **kwargs are special syntaxes used in function definitions to allow a variable number of arguments to be passed to a function.

*args (Non-keyword Arguments):

  • The *args parameter allows a function to accept any number of non-keyword arguments.
  • The * before args tells Python to unpack the arguments passed to the function and store them in a tuple named args.
  • You can name the parameter *args anything you like, but the * symbol is necessary to indicate variable-length arguments.
  • Example:
def my_function(*args):
    for arg in args:
        print(arg)

my_function(1, 2, 3)  # Prints: 1 2 3
my_function('Hello', 'World')  # Prints: Hello World

**kwargs (Keyword Arguments):

  • The **kwargs parameter allows a function to accept any number of keyword arguments, which are typically specified as key-value pairs.
  • The ** before kwargs tells Python to unpack the keyword arguments passed to the function and store them in a dictionary named kwargs.
  • Similar to *args, you can choose any name for the parameter, but the ** symbol is required.
  • Example:
def my_function(**kwargs):
    for key, value in kwargs.items():
        print(key, value)

my_function(name='Alice', age=25)  # Prints: name Alice, age 25
my_function(city='New York', country='USA')  # Prints: city New York, country USA

*args and **kwargs together in a function definition:

It’s worth noting that you can use *args and **kwargs together in a function definition. In such cases, *args captures non-keyword arguments, while **kwargs captures keyword arguments.

def print_arguments(*args, **kwargs):
    for arg in args:
        print("Positional argument:", arg)
    
    for key, value in kwargs.items():
        print("Keyword argument -", key + ":", value)

print_arguments(1, 2, 3, name='Alice', age=25)

In this example, the print_arguments function accepts a variable number of positional arguments using *args and a variable number of keyword arguments using **kwargs. It then iterates over the positional arguments and prints each one, and also iterates over the keyword arguments and prints the key-value pairs.

Positional argument: 1
Positional argument: 2
Positional argument: 3
Keyword argument - name: Alice
Keyword argument - age: 25
Python
Python Programming
Args And Kwargs
Args Python
Kwargs
Recommended from ReadMedium