What does the *args and **kwargs mean in Python functions?

95 views

What does the *args and **kwargs mean in Python functions?

What does the *args and **kwargs mean in Python functions?

solveurit24@gmail.com Changed status to publish February 13, 2025
0
  • *args (pronounced “star args”):
    • Allows a function to accept any number of positional arguments.
    • Inside the function, args is treated as a tuple containing all the arguments.
  • **kwargs (pronounced “double star kwargs”):
    • Allows a function to accept any number of keyword arguments.
    • Inside the function, kwargs is treated as a dictionary containing all the keyword arguments.

Example:

def example_function(*args, **kwargs):
    print("Args:", args)
    print("Kwargs:", kwargs)

example_function(1, 2, 3, name="Alice", age=30)
# Output:
# Args: (1, 2, 3)
# Kwargs: {'name': 'Alice', 'age': 30}

solveurit24@gmail.com Changed status to publish February 13, 2025
0