Way to pass multiple parameters to a function in python

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

提交回复
热议问题