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.

    2 answers 2

    In fact, I use args and kargs only for parsing command line parameters (well, this is understandable). I do the rest of the functions exclusively as foo (maybe I just didn’t have functions with a huge number of parameters).

     def collect_object(client, param_object, view, args, wait_thread = None, stdin_passwd = False): 

    But no, I even found one like this:

     def set_paths (cls, data_path, certbase, serv_certbase, rights, \ group_rights, sids, pids, sids_pids, cert_path, log_filename, \ cert="server.crt", key="server.key"): 

    This helps when changing parameters, since I will not skip these parameters when calling a function. Yes, and visibility, self-documenting. And about "when the number of function arguments is not clear" - I haven’t had one yet. In addition, many ide suggest what parameters are not used, which is also sometimes useful =)

    • Thank. Here is the kasamo IDE, when using intelli-sense a method or function is displayed with args and kargs you have to either watch the sources or the documentation. - mind_mixer
    • Yes, this is especially unpleasant if you need to quickly make a small edit to the code that has not been seen for half a year. - spirit
    • I have it when I work with wxPython or matplotlib - mind_mixer

    There are cases of using both *args and **kwargs . On the move, I will only remember django - there is a query to the database through kwargs.

     users.objects.filter(name='Иван', birdthday__year=1985) 

    In a natural way, the filter function receives arguments through *args and **kwargs , and, as I recall, both through *args and **kwargs . I do not remember how it was made there.

    Whats up difference

     foo(101, 'Max', 19, 'max@somedomain.org') 

    and

     foo(id=101, name='Max', age=19, email='max@somedomain.org') 

    the second way makes sense when the arguments are predefined. For example:

     def foo(id=0, name=None, age=20, email=None): # some magic foo(name="Max", email="max@example.com")