def total(initial=5, *numbers, **keywords): count = initial for number in numbers: count += number for key in keywords: count += keywords[key] return count print(total(10, 1, 2, 3, vegetables=50, fruits=100)) 

Please, tell me, what does the number of the parameter varArgs * numbers?

  • In this line for number in numbers: what is the initial value of number? 10 I understand, but in the next cycle 1 and so on? And key in the first cycle 50, and then 100? - arnold
  • No, the first number value will be 1. 10 will fall into initial - LinnTroll

2 answers 2

Strange question, what does it mean with what?

you pass arguments to the function, the first of which is the initial value. Then there are some unnamed arguments, and only then - some named arguments.

In the function, you assign the initial value to the variable count. Then go through the numbers - a list of unnamed arguments (i.e. simply) numbers and add them. Then do the same with the named arguments.

another thing is that the function is written non-optimally and can be rewritten into one line:

 def total(initial=5, *args, **kwargs): return initial + sum(args) + sum(kwargs.values()) 
  • > suboptimally. Most likely, it is painted precisely in order to clarify the essence of the transfer of parameters. The author also needed help with explanations. - etki

* numbers - An arbitrary number of arguments. All arguments will be placed in the list, that is, in (1,2,3,4), for example, an example total(10, 1, 2, 3, vegetables=50, fruits=100) will return

 10 (1, 2, 3) {'vegetables': 50, 'fruits': 100} 

** keywords - An arbitrary number of named arguments.