+ 2

what are the differences between *par , **par and *arg , **arg in python?

what's the difference between *par , **par when we declare a function? and *arg , **arg when we call a function?

13th Apr 2019, 3:04 PM
khalil Saghiri
khalil Saghiri - avatar
2 Réponses
+ 11
*args and **kwargs are special syntaxes for function arguments. With *args ( can be anything instead of the word 'args' , it's just a convention) you tell the function to accept variable number of non-keyworded arguments. See this example: def func(*agrs): for each in *args: print(each) Or you can also use it to take any number of extra arguments( even 0) other than your explicitly required arguments. Ex: def func(one, two, *argv) : pass func(1,2,3,4,5) Here, one will take 1, two will take 2 and remaining other, 3,4,5, will be in *argv through which you can iterate. **kwargs( or anything other than the word 'kwargs', just a convention) works the same way except it takes keyworded arguments. Understand it like sending a key-value pair to the function. Ex: def func(arg1, arg2, **kwargs) : for key, value in kwargs: print(key, value) func('1', '2', a=1,b=2, c=3) Here arguments other than '1' and '2' will get in the kwargs. *par, **par, *arg, **arg are, again, just conventions.
13th Apr 2019, 3:44 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
thank you so much!
13th Apr 2019, 4:45 PM
khalil Saghiri
khalil Saghiri - avatar