Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
*args | Arguments in Function
Intermediate Python: Arguments, Scopes and Decorators

*args*args

In Python, you can add any number of arguments to a function, and *args and **kwargs can be helpful.

We should remember that asterisks * help in packing and unpacking values.

Let’s use the same add function as in the previous chapter but with the use of *args.

The difference is that you can pass any number of arguments to the function; even if you do not pass any arguments, the result will be 0, just like a calculator in your phone.

The function add receives any number of arguments and packs them into a tuple with the variable name args. The for loop goes through the tuple values and adds them to the result variable in the body of the function.

Note

You can use not only *args but any name. However, it is best practice to use the name args for arbitrary arguments.

Arbitrary arguments must appear after the positional and optional arguments.

Все було зрозуміло?

Секція 2. Розділ 2
course content

Зміст курсу

Intermediate Python: Arguments, Scopes and Decorators

*args*args

In Python, you can add any number of arguments to a function, and *args and **kwargs can be helpful.

We should remember that asterisks * help in packing and unpacking values.

Let’s use the same add function as in the previous chapter but with the use of *args.

The difference is that you can pass any number of arguments to the function; even if you do not pass any arguments, the result will be 0, just like a calculator in your phone.

The function add receives any number of arguments and packs them into a tuple with the variable name args. The for loop goes through the tuple values and adds them to the result variable in the body of the function.

Note

You can use not only *args but any name. However, it is best practice to use the name args for arbitrary arguments.

Arbitrary arguments must appear after the positional and optional arguments.

Все було зрозуміло?

Секція 2. Розділ 2
some-alt