Python variable-length parameters (non-keyword and keyword parameters)

Source: Internet
Author: User

Variable-length parameters exist in the sense that each time a function is called to handle different amounts of parameter input. That is, the parameter number entered before the call is unknown, or multiple calls to the function, each time the amount of input parameters is inconsistent;

Variable-length parameters are classified as non-keywords and keyword types, corresponding to tuples and dictionaries, defining a class as follows, and the function is to print out the input function:

1>>>classTest:2     defKeyword (self,a,b,*non_keyword,**keyword):3         Print('a is', a)4         Print('b is', B)5         Print('Non_keyword', Non_keyword)6          forKeyinchKeyword:7             Print("'%s ':%s"%(Key,str (Keyword[key] )))8>>> test =Test ()9>>> dis = Test.keyword ('a','b',('C','D'), e='e', f='F')TenA isa OneB isb ANon_keyword (('C','D'),) - 'e': E - 'F': F

When the input is entered at one time, when it is called again, the input of a different number of parameters can still be printed out:

1>>> test0 =Test ()2>>> Dis0 = Test0.keyword ('a','b',('C','D','C0','C1','D0','D1'), e='e', f='F', g='g', h='h')3A isa4B isb5Non_keyword (('C','D','C0','C1','D0','D1'),)6 'h': H7 'e': E8 'F': F9 'g': G


Therefore, variable length parameter input has some flexibility and can be used to deal with different scenarios.

Python variable-length parameters (non-keyword and keyword parameters)

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.