r/pythontips • • Mar 08 '19

​Python *args and **kwargs Made Easy

*args and **kwargs may seem scary, but the truth is that they are not that difficult to grasp and have the power to grant your functions with flexibility and readability.

​Python *args and **kwargs Made Easy

50 Upvotes

9 comments sorted by

11

u/basalamader Mar 08 '19

OP solid work. One change that I hope to impact is when you use *.get() for dicts, you can set a default in case the key you are looking for is not found.

so your first method would be one line of code:

error = f"{key} does not exist"

kwargs.get(key, error)

1

u/brews Mar 09 '19

At what point do you use this over making an actual kwarg?

2

u/basalamader Mar 09 '19

What do you mean?

1

u/brews Mar 09 '19

In what cases would you use get() on **kwargs with a default return value, rather than just giving the function signature that keyword argument and default?

3

u/basalamader Mar 09 '19 edited Mar 09 '19

So when you use get() on **kwargs you are simply getting a value based on a key. The data structure behind the kwargs is a dict so you have two options

option 1:

def getKey(key, **kwargs):

    return kwargs[key]

in this case, if the key doesnt exist in the kwargs, then you will get a KeyError. One way to safe guard against that is to do what OP did

def getKey(key, **kwargs):

    if key in kwargs:

        return kwargs[key]

    return f '{key} not in kwargs'

but to make this even better, the get() method of the dict class, gets the value based on whether the key exists or not. If the key does not exist, it returns a default value and you can control that default. So the code above now becomes,

def getKey(key, **kwargs):

    return kwargs.get(key, f '{key} not in kwargs')

Simpler, right?

1

u/brews Mar 09 '19

Right, I get that and you explain it very well.

My question is why you would use this:

def getkey1(key, **kwargs):
     return kwargs.get(key, 'foobar')

Because if you have key, you could just:

def getkey2(key='foobar', **kwargs):
    return key

If I were to guess (humbly) at an answer to my own question, it's that in getkey1(), key is from user input but with getkey2() is hard coded...

okay, now I see how getkey1() could be more pythonic... I'm just slow today. Thanks for the input.

1

u/basalamader Mar 09 '19

Also in your example of getKey2() you are returning the key not the value of the key contained within the kwargs. In other words, you are passing in a user defined param and returning that param back without checking the kwargs dict for the value

4

u/[deleted] Mar 08 '19

If I could add upvote multiple times I would do it now... simple, right to the point. I Finally understand them thanks to you... refactoring coming right up :)

1

u/TotesMessenger Mar 09 '19

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)