Python---function---keyword parameters

Source: Internet
Author: User

The #  keyword parameter #  variable parameter allows you to pass in 0 or any parameter, which is automatically assembled as a tuple#  when called, and the keyword parameter allows 0 or any parameter with parameter names to be passed in. These keyword parameters are automatically assembled inside the function as a Dictdef person (name, age, **kw):     print (' Name: ',  name,  ' Age: ', age,  ' other ',  kw) the #  function person accepts the keyword parameter kw in addition to the required parameter name and age. #  when calling this function, you can pass in only the required parameters person ("Yel",  23) #  can also pass in any number of keyword parameters person (' Bob ',  35, city= ' Beijing ') ) person (' Adam ',  45, gender= ' M ',  job= ' Engineer ') # ----------------------------------------- ------------------------------------------------------#  keyword parameter can extend function #  For example, in the person function, We are guaranteed to receive both the name and the age parameters, but if the caller is willing to provide more arguments, we can also receive #  imagine being doing a user registration function, in addition to the user name and ages are required, the others are optional, Use keyword parameters to define this function to meet the requirements of registration # -------------------------------------------------------------------------------- ---------------#  is similar to variable parameters, you can also assemble a dict, and then convert the dict into a keyword parameter to extra = {' city ':  ' Beijing ',   ' job ':  ' Engineer '}person (' Jack ',  24, city=extra[' city '], job=extra[' job ']) #  the above complex call can be simplified notation # **extra to extra this dict all key-value with the keyword parameter passed to the **KW parameter of the function, KW will get a dict, Note that the dict obtained by KW is a copy of extra, and the changes to KW do not affect the Extraperson outside the function (' Jack ',  24, **extra)


Python---function---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.