[Road to Intermediate] What are Python's * args, ** kwargs?

Link to summary

https://qiita.com/ganariya/items/fb3f38c2f4a35d1ee2e8

Introduction

If you look closely at the Python code

def f(*args, **kwargs):
    pass

args with * mark And I see kwargsmarked with**.

What is this? ??

What does it stand for in the first place?

What do args and kwargs stand for?

In English, the argument is called ʻargument`, and if there are multiple arguments, it is naturally arguments. Python is a notation that is often abbreviated

--args = abbreviation for arguments Variable length tuple --kwargs = abbreviation for keyword arguments Keyword dictionary

It means that.

*args

* args is a notation that allows you to receive the ** extra keywordless arguments ** passed by the function in a single tuple.

For example, consider the function accumulate, which adds and returns a number of given integers:

def accumulate(*args):
    print(args)
    return sum(args)

'''
(10, 20, 30, 40)
100
'''
print(accumulate(10, 20, 30, 40))

Since there are many arguments given to accumulate, they are stored as tuples in * args.

By preparing an argument with one "asterisk" like * args in this way, you can receive the remaining argument without keyword specification.

In addition, tuples and list iterables can be ** unpacked ** by reapplying the "asterisk".

def accumulate(*args):
    print(*args)
    return sum(args)

'''
10 20 30 40
100
'''
print(accumulate(10, 20, 30, 40))

Therefore, you can use this unpack to pass it to other functions as many times as you like.

def accumulate(*args):
    return sum(args)


def some_calc(*args):
    
    #Unpack and pass to accumulate
    return accumulate(*args)


print(some_calc(10, 20, 30, 40))

**args

** args is a keyword specification argument that receives ** dictionaries together with other arguments that do not apply **.

def f(x, y, **kwargs):
    print(x)
    print(y)
    print(kwargs)


'''
10
20
{'a': 20, 'b': 'hello'}
'''
f(x=10, y=20, a=20, b="hello")

In the above, x and y flow there because they already exist in the argument of the function f, but unfortunately a and b with no receiver are messed up by kwargs.

Dictionaries can also be unpacked.

If you use only one "asterisk", you can retrieve only the key. And if you use two, it will look like a = 20, b = "hello".

def f(x, y, **kwargs):
    print(x)
    print(y)
    print(*kwargs)

    #print(**kwargs)


'''
10
20
a b
'''
f(x=10, y=20, a=20, b="hello")

Unfortunately, print (** kwargs) doesn't work. I get an error. For python print This is because the notation print (a = 20, b =" hello ") is not provided.

Finally

* args, ** kwargs is a notation that can only be done in a dynamic language, This is often used in Python packages such as def init. This allows you to receive user-given parameters in dictionaries and tuples, allowing many behaviors.

When used with vars, it can perform very useful behaviors. I'll deal with that in another article.

Recommended Posts

[Road to Intermediate] What are Python's * args, ** kwargs?
[Road to intermediate level] Utilize Python's built-in function vars
Road to Linux Intermediate: Network Edition
[Road to Intermediate] Understanding Python Properties
[Road to intermediate Python] Use ternary operators
[Road to intermediate Python] Use lambda expressions
[Road to intermediate Python] Article link summary
[Road to Intermediate] Python seems to be all objects
What are python tuples and * args after all?
[Road to Python Intermediate] Define __getattr__ function in class
[Road to intermediate Python] Define in in your own class