What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
In Python, *
unpacks iterables into positional arguments, while **
unpacks dictionaries into keyword arguments for function invocation.
Give *
a whirl:
Now, let **
take center stage:
Unleashing argument flexibility with *args
and **kwargs
In function definition, *args
slurps additional positional arguments into a tuple, and **kwargs
sucks in additional keyword arguments into a dictionary. These argument packers allows for flexible parameter passing, making your functions incredibly adaptable.
Function with packed house:
This is a party for everyone:
Outputs:
Attendees: ('Alice', 'Bob')
Arrangements: {'flowers': 'Roses', 'music': 'Jazz'}
Python 3+: Applauding keyword-only arguments
Python 3's feature of *
in function parameters enforces keyword-only arguments after it. It's like Python intervening, "Hey, let's be explicit about which arguments we're talking about!".
Like putting a keeper at your castle gate:
So, you must knock it with names:
Python 3+: Enhanced iterable unpacking
With *
in iterable assignment, collect all extra items that don't have their own dedicated variables. It's like your mum who ensures nobody gets left behind!
Preserved dictionary ordering from Python 3.6+
As of Python 3.6, dictionaries are well-behaved kids that remember the order of their elements. This behavior impacts the ordering of **kwargs
.
Decorators: Unpacking superheroes with *args
and **kwargs
*args
and **kwargs
are like Swiss army knives in decorators, helping you wrap any function or method regardless of their signature.
A simplistic decorator, just for kicks:
Best practices: Relishing *args
and **kwargs
While *args
and **kwargs
wield immense power, with great power comes the responsibility to use these tools judiciously. Below are some pointers:
- Choice of Names:
args
andkwargs
are convention. They're the Pythonistas' sweetheart. Stick with them unless you really have to "paint the bike shed". - Selectivity: Resist the urge to go on a "one
*args
, one**kwargs
, one function fits all" crusade. They're best employed when argument requirements are truly flexible. - Documentation: Be a sport, give your fellow devs a detailed account of your function, especially if it hosts a
*args
and**kwargs
party inside.
Was this article helpful?