Python args and kwargs: Demystified (Summary)
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
You’re now able to use *args
and **kwargs
to accept a changeable number of arguments in your functions. You’ve also learned something more about unpacking operators. To recap, in this tutorial you’ve learned what *args
and **kwargs
actually mean, how to use *args
and **kwargs
in function definitions, how to use a single asterisk (*
) to unpack iterables, and how to use two asterisks (**
) to unpack dictionaries. And that just about wraps up this tutorial.
00:26 I hope you enjoyed it, and thanks very much for watching!
Mallesham Yamulla on Jan. 13, 2020
Many Thanks for this video turorial, it’ a great refresher for args and kwargs.
Pygator on Jan. 18, 2020
Really liked the list merging and dictionary merging examples!
ColmC on Jan. 18, 2020
Very handy. Thanks!
alanhrosenthal on Feb. 3, 2020
very good!
Suggestion - maybe add how to pass arguments from the command line including variable numbers of args from the command line.
Geir Arne Hjelle RP Team on Feb. 3, 2020
Alan, thanks for the suggestion.
We actually have a full article coming out on Wednesday on the topic of command line arguments!
You should be able to see it already in your membership preview: click your profile picture, then notifications. Scroll down to find Python Command Line Arguments
mohammedayub44 on Feb. 25, 2020
No hard-coded values ever in my scripts again ! Nice tricks with just one operator. It would be good to explain runtime complexity as well when using such iterables.
Eriberto on March 13, 2020
Any more practical use-cases for these examples? Just re learning these things again.
pshapard on April 8, 2020
Rich, thanks for the video course. I understand what’s going on the args and kwargs. What if you have a simple function like the below. How would implement args to this function.
def get_auth_token(): login = LoginCreds(ip, api, username, password) try: auth = login.LoginToken(ip, api, username, password) return auth except ConnectionError as e: logging.info(“Could not connect to host”) sys.exit(0)
pshapard on April 8, 2020
regarding my last post. I have written scripts using the requests module extensively. The responses are large json the jsons are a dictionary of dictionaries. I parse those to get certain information, such as,
permittedInterconnectTypeUri = [‘/rest/interconnect-types/ce3381c9-c948-4c71-946a-8893163ae4a6’] networkUris = [‘/rest/fc-networks/0b20f7fc-370d-4163-b8a6-dc06442f6657’]
These URI need to be placed in a json payload to send to the appliance.
How would i use args and *kwargs in this scenario.
Mark on June 14, 2020
Amazing!
mahlenius on July 16, 2020
THanks, now I understand these C-like symbols I have seen in argument lists of other code.
mortenelund on July 20, 2020
I needed an update on this and I have not seen any better! Thanks!
Agil C on Aug. 1, 2020
Thanks, very useful..!
Danadasa Chan on July 7, 2023
Thank you for this tutorial. Very helpful.
I do have a question. On slide 14, you stated:
“5.
**
can only be used on dictionaries”
and in the concatenate example, the function says:
def concatenate(**kwargs):
and the function call says:
concatenate(a="Real", b="Python", c="is", d="great!")
The input argument is not a dictionary, rather these are named arguments.
Can you please clarify?
Bartosz Zaczyński RP Team on July 7, 2023
@Danadasa Chan The double star operator (**
) has a double meaning in Python, depending on where you use it:
- You can mark a formal parameter in a function with
**
to indicate that any further named parameters will be gathered into a dictionary, typically namedkwargs
to mean keyword arguments. - When calling a function that expects keyword arguments, you can unpack an existing dictionary into key-value pairs.
Become a Member to join the conversation.
tsusadivyago on Jan. 8, 2020
We learn python basic properly in this site :)