Python Object-Oriented Python Inheritance Multiple Superclasses

arash shabanpour
arash shabanpour
5,915 Points

i didn't understand what *args is

what *args argument actually do?

Dave StSomeWhere
Dave StSomeWhere
19,792 Points

It allows you to pass an unknown number of arguments to a function and/or receive an unknown number of parameters into a function.

Here's a pretty good explanation in the answer of this SO Post

1 Answer

Duc Bui
Duc Bui
14,546 Points

Sometimes when you define a function and you don't know how many arguments you and someone else using your codes are gonna need, you use *args. For example:

def multiply(*args):
    z = 1
    for num in args:
        z *= num
    print(z)

multiply(4, 5)
multiply(10, 9)
multiply(2, 3, 4)
multiply(3, 5, 10, 6)

Output 20 90 24 900

Duc Bui
Duc Bui
14,546 Points

Same idea for **kwargs.

Ewerton Luna
Ewerton Luna
Full Stack JavaScript Techdegree Student 15,138 Points

okay, but he never uses the args inside the code block, so why even put it in the function definition at all?

Duc Bui
Duc Bui
14,546 Points

Ewerton Luna he never uses it doesnt mean someone else won't, u should think of your codes are gonna be used by other coders as well.

I think Kenneth should make an example out of it (maybe he did, I just dont remember). I know it is hard to grasp some concepts in this series, I did not understand them at first either but stick to it.

Good luck and happy coding!