Python implements functions that accept any number of arguments

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.