Friends, especially those who program in Python, would like to discuss the next question, which I think is more related to beginners, but still. I would like to hear some explanations or recommendations: in which case which method of passing arguments when calling a function or method is better to use and / or use you in your projects. And how you decide how to declare a function at all. Below is a small example:
def foo(id, name, age, email): print id print name print age print email foo(101, 'Max', 19, 'max@somedomain.org') print '------' foo(id=101, name='Max', age=19, email='max@somedomain.org') print '------' def bar(**kargs): print kargs['id'] print kargs['name'] print kargs['age'] print kargs['email'] bar(id=101, name='Max', age=19, email='max@somedomain.org')
The foo
function is declared in the standard way with arguments id
, name
, age
, email
. The bar function is declared a little differently, or rather, the method of passing arguments here using **kargs
(I also know about *args
, but decided not to give an example here) The most banal recommendation that follows from my logic is: when the number of function arguments is not clear ( It would be great to give examples from real projects when this is true) or a lot of arguments - it is better to use ** kargs. But at the same time, I think there is one drawback, in terms of self-documenting code. Those. in order to understand which arguments can be passed to a function, it is necessary that docsting is written (or simply third-party documentation) or the implementation of the function should be studied and, on the basis of it, we can draw conclusions on what can and cannot be passed. I apologize for some confusion of the question, but I hope the essence of what I ask, I stated.