Way to pass multiple parameters to a function in python

前端 未结 6 1761
遇见更好的自我
遇见更好的自我 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:35

    You could use *args if you don't need to use names for your lists:

    def WorkDetails(link, *args):
        if args[0] == ... # Same as if AllcurrValFound_bse == ...
            ...
    
     # Call the function:
     WorkDetails(link, AllcurrValFound_bse, AllyearlyHLFound_bse, AlldaysHLFound_bse, AllvolumeFound_bse, AllprevCloseFound_bse, AllchangePercentFound_bse, AllmarketCapFound_bs)
    

    Or you could use a dictionary

    def WorkDetails(link, dict_of_lists):
        if dict_of_lists["AllcurrValFound_bse"] == ...
            ...
    
    # Call the function
    myLists = {
        "AllcurrValFound_bse": AllcurrValFound_bse,
        "AllyearlyHLFound_bse": AllyearlyHLFound_bse,
        ...,
        ...
    }
    WorkDetails(link, myLists)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 09:39

    test function:

    You can use multiple arguments representing by *args and multiple keywords representing by **kwargs and passing to a function:

    def test(*args, **kwargs):
        print('arguments are:')
        for i in args:
            print(i)
    
        print('\nkeywords are:')
        for j in kwargs:
            print(j)
    

    Example:

    Then use any type of data as arguments and as many parameters as keywords for the function. The function will automatically detect them and separate them to arguments and keywords:

    a1 = "Bob"      #string
    a2 = [1,2,3]    #list
    a3 = {'a': 222, #dictionary
          'b': 333,
          'c': 444}
    
    test(a1, a2, a3, param1=True, param2=12, param3=None)
    

    Output:

    arguments are:
    Bob
    [1, 2, 3]
    {'a': 222, 'c': 444, 'b': 333}
    
    keywords are:
    param3
    param2
    param1
    
    0 讨论(0)
  • 2021-02-06 09:44

    You can change it to:

    def WorkDetails(link, details):
    

    Then invoke it as:

    details = [ AllcurrValFound_bse, AllyearlyHLFound_bse, 
                AlldaysHLFound_bse, AllvolumeFound_bse, 
                AllprevCloseFound_bse, AllchangePercentFound_bse, 
                AllmarketCapFound_bse ]
    workDetails(link, details)
    

    And you would get the different values out of details by:

    AllcurrValFound_bse = details[0]
    AllyearlyHLFound_bse = details[1]
    ...
    

    It would be more robust to turn details into a dictionary, with the variable names as keys, so take your pick between a few more lines of code vs. defensive programming =p

    0 讨论(0)
  • 2021-02-06 09:44

    That you need to pass that many lists is a sign that your function is not doing just one thing and you should refactor it by breaking it into smaller functions and/or converting it to a class. You can pass parameters as keywords, or pass an arbitrary number of parameters to a function, as described by this StackOverflow page, but it'll still be hard for others to read and understand if you have your function do more than one thing.

    0 讨论(0)
  • 2021-02-06 09:59

    I think that usage of **kwarg is better. Look this example:

    def MyFunc(**kwargs):
        print kwargs
    
    
    MyFunc(par1=[1],par2=[2],par3=[1,2,3])
    
    0 讨论(0)
提交回复
热议问题