DEV Community

M-Rafay
M-Rafay

Posted on

🐍 Mastering Python: The Art of *args and **kwargs πŸš€πŸ’»

Unlock the Elegance of Pythonic Functionality!

In the Python universe, *args and **kwargs are like secret weapons, providing unparalleled flexibility. Here's why you should embrace them:

✨ Dynamic Flexibility with *args:

def dynamic_function(*args):
    for arg in args:
        print(arg)

dynamic_function(1, "hello", True)
# Output: 1
#         hello
#         True
Enter fullscreen mode Exit fullscreen mode

Embrace variable non-keyword arguments for functions, allowing you to handle an arbitrary number of parameters effortlessly.

🌟 Keyword Magic with **kwargs:

def keyword_function(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

keyword_function(name="John", age=25, city="New York")
# Output: name: John
#         age: 25
#         city: New York

Enter fullscreen mode Exit fullscreen mode

Leverage **kwargs for handling dynamic keyword arguments, enabling your functions to receive named parameters flexibly.

πŸ”— Enhanced Function Signatures:

def dynamic_signature(*args, **kwargs):
    print("Non-keyword arguments:")
    for arg in args:
        print(arg)

    print("\nKeyword arguments:")
    for key, value in kwargs.items():
        print(f"{key}: {value}")

dynamic_signature(1, "hello", True, name="John", age=25)
# Output: Non-keyword arguments:
#         1
#         hello
#         True
#
#         Keyword arguments:
#         name: John
#         age: 25

Enter fullscreen mode Exit fullscreen mode

Embrace Pythonic Coding: Elevate your Python game by incorporating *args and **kwargs into your code. It's the key to clean, flexible, and elegant solutions!

Ready to level up your Pythonic skills? πŸš€ Share your thoughts and favorite Python tricks! πŸ’¬πŸπŸ’‘

Python #CodingSkills #PythonProgramming #TechInnovation

Top comments (6)

Collapse
 
megaproaktiv profile image
Gernot Glawe

Nicely explained! But I think it is an antipattern not to name arguments. If you have a function ,it behaves in a defined way with defined parameters. So I want to let the consumer know what parameter in which type are expected.

Collapse
 
afikbenshimol profile image
Afik Ben Shimol

The most annoying functions in Python are the ones with kwargs. Why? Because you have to somehow know every keyword and every value it accepts in order to actually get what you want. And guess what, the docstring does not have it and the online documentation (if there is one) have it scattered between 100 different pages. matplotlib is one of the worst libraries in that regard (but also because it does not use a single standard for its functions’ input).

In the end it’s like some black magic voodoo knowledge that passes only through tips from other devs and stack overflow questions.

So please don’t use kwargs.

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

I agree with @megaproaktiv that *args and **kwargs are antipattern. Use cases, I see, are when writing decorators or tools that extends others tools that we have no control.

We should master Python by not allowing users to pass anything onto our function/methods. We should be very verbose on types and even order of arguments (e.g. using / and *)

Unlock the Elegance of Stricken & Embrace * and / with strong typing πŸ’¬ πŸ¦™

Collapse
 
mursalfk profile image
Mursal Furqan Kumbhar

Thank you for sharing. Nicely explained and very well structured.

Collapse
 
volodyslav profile image
Volodyslav

Nice article 😁

Collapse
 
niksheridan profile image
Nik Sheridan

nice, a kwarg a day keep the cobwebs at bay!