Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Python args and kwargs: Demystified (Summary)

Download

Sample Code (.zip)

5.8 KB
Download

Course Slides (PDF)

261.9 KB

tsusadivyago on Jan. 8, 2020

We learn python basic properly in this site :)

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:

  1. You can mark a formal parameter in a function with ** to indicate that any further named parameters will be gathered into a dictionary, typically named kwargs to mean keyword arguments.
  2. 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.