Way to pass multiple parameters to a function in python

前端 未结 6 1760
遇见更好的自我
遇见更好的自我 2021-02-06 09:12

I have written a python script which calls a function. This function takes 7 list as parameters inside the function, something like this:

def WorkDetails(link, A         


        
6条回答
  •  清酒与你
    2021-02-06 09:38

    Usually, passing more than 3 parameters to a function is not recommended. This is not specific to python but to software design in general. You can read more about how to reduce the number of parameters passed to a function here.

    Following the perspective of previous answers, but from a more general point of view I would like to add that there are several ways to make your code more readable:

    • to divide your function in simpler ones which have fewer arguments (define a function that takes your variable link, specific_list, list_type. By doing this you can detect within your WorkDetails function which list you passed with list_type and add the correct elements to that specific list)
    • to create a parameter object/data structure which is passed to your function (this is what previous answers suggested using lists, dictionaries...)

    Hope this help.

提交回复
热议问题