Define Functions:keyword Arguments

Source: Internet
Author: User

Functions can also be called using keyword arguments of the form kwarg=value. For instance, the following function:

DefParrot(Voltage,State=' A stiff ',Action=' Voom ',Type= ' Norwegian Blue ' ): print ( "--this parrot wouldn ' t" actionend< Span class= "o" >= ") print (" if You put "voltage" volts through it. " ) print ( "-lovely plumage," type print (state "!" )               /span>                

Accepts one required argument (voltage) and three optional arguments (State, action, and ty PE). This function can is called in any of the following ways:

Parrot(1000)# 1 Positional argumentParrot(Voltage=1000)# 1 keyword argumentParrot(Voltage=1000000,Action= ' vooooom ' ) # 2 keyword argumentsparrot  (action= ' vooooom '  voltage=1000000) # 2 keyword Argumentsparrot ( ' A million '  " Bereft of life '  ' jump '  # 3 positional arguments< span class= "n" >parrot ( ' a thousand ' state = ' pushing up the Daisies ' ) # 1 positional, 1 keyword
                                                                    

But all the following calls would is invalid:

Parrot()                     # required argument missingparrot(voltage=5.0' dead '# Non-keyword Argument after a keyword argumentparrot(voltage= +Duplicate value for the same ArgumentParrot(actor=' John Cleese '# unknown keyword argument    

In a function call, keyword arguments must follow positional arguments. All the keyword arguments passed must match one of the arguments accepted by the function (e.g. actor was not a VA Lid argument for the parrot function), and their order was not important.

1 The keyword parameter is after the position parameter;

2 The order of the keyword parameters can be arbitrary;

Define Functions:keyword Arguments

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.