Below for you to share a Python implementation to accept arbitrary number of parameters of the function method, has a good reference value, I hope to be helpful to everyone. Come and see it together.
This function is not how much I urgently need function, just happen to see, think maybe later will use. The function is that the implementation function can accept different numbers of parameters.
In fact, in the C language This function is familiar, although the implementation of the form is not the same. The main function in C is a similar function that can be implemented in such a way as to implement a program that supports command-line arguments.
First, write a Python demo code that implements the appropriate functionality:
Deffuncdemo (*par): print ("Number of pars:%d"%len (PAR)) print ("Type of Par:%s"%type (PAR)) i = 0 if len (PAR)! = 0: For p in par: i = i + 1 print ("%d par is:%s"% (i,p))
Run test interaction records after loading:
>>>funcdemo () Number of pars:0type of par:<class ' tuple ' >>>>funcdemo () Number of pars: 3type of Par:<class ' tuple ' >1 par is:12 par is:23 par Is:3>>>funcdemo (all in A, ' abc ') Number of Pars:4type of Par:<class ' tuple ' >1 par is:12 par is:23 par is:34 par is:abc
This is basically the way that Python implements the arbitrary parameter function and the application, and then summarizes the corresponding knowledge.
It is simpler to implement the function that Python accepts any number of parameters. is to precede the parameter with an asterisk so that the corresponding parameter position can accept any parameter. The corresponding parameter is a tuple in the function and can be seen from the result of the interaction above.
In fact, this feature can also support the introduction of dictionaries. If the dictionary is passed in, then it is necessary to pass in a pair of parameters.
Temporarily, this function in my work and life is not very useful, as a back-up function to see the effect of it!