Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Python args and kwargs: Demystified (Summary)

Download

Sample Code (.zip)

5.8 KB
Download

Course Slides (PDF)

261.9 KB
Avatar image for tsusadivyago

tsusadivyago on Jan. 8, 2020

We learn python basic properly in this site :)

Avatar image for Mallesham Yamulla

Mallesham Yamulla on Jan. 13, 2020

Many Thanks for this video turorial, it’ a great refresher for args and kwargs.

Avatar image for Pygator

Pygator on Jan. 18, 2020

Really liked the list merging and dictionary merging examples!

Avatar image for ColmC

ColmC on Jan. 18, 2020

Very handy. Thanks!

Avatar image for alanhrosenthal

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.

Avatar image for Geir Arne Hjelle

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

Avatar image for mohammedayub44

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.

Avatar image for Eriberto

Eriberto on March 13, 2020

Any more practical use-cases for these examples? Just re learning these things again.

Avatar image for pshapard

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)

Avatar image for pshapard

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.

Avatar image for Mark

Mark on June 14, 2020

Amazing!

Avatar image for mahlenius

mahlenius on July 16, 2020

THanks, now I understand these C-like symbols I have seen in argument lists of other code.

Avatar image for mortenelund

mortenelund on July 20, 2020

I needed an update on this and I have not seen any better! Thanks!

Avatar image for Agil C

Agil C on Aug. 1, 2020

Thanks, very useful..!

Avatar image for Danadasa Chan

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?

Avatar image for Bartosz Zaczyński

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.