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.
Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Python args and kwargs: Demystified (Summary)
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!
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 namedkwargsto 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.
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.