Python---function---named keyword parameter

Source: Internet
Author: User

#  named keyword parameter #  for keyword arguments, the caller of the function can pass in any unrestricted keyword argument #  as to exactly what is passed in, it is necessary to check the inside of the function through the KW #  still take the person () function as an example, We want to check if there is a city and job parameter Def person (name, age, **kw):    if  ' City '  in  kw:        #  has city parameters          pass    if  ' Job '  in kw:         #  have job parameter         pass    print (' Name: ', name,  ' Age: ', age,  ' other: ',  kw ' #  but the caller can still pass in the Unrestricted keyword parameter person (' Jack ',  24, city= ' Beijing ',  addr= ' Chaoyang ',  zipcode=123456) #  if you want to restrict the name of the keyword argument, you can use the named keyword argument, for example, Only receive city and job as keyword parameter Def person (name, age, *, city, job):     print ( Name, age, city, job) #  is different from the keyword argument **kw, the named keyword parameter requires a special delimiter *,* the parameters that follow are treated as named keyword Parameters #  called as follows person ( ' Jack ',  24, city= ' Beijing ', &Nbsp;job= ' Engineer ') the #  named keyword parameter must pass in the parameter name, which differs from the positional parameter. If the parameter name is not passed in, the call will be Error # typeerror: person ()  takes 2 positional arguments but  4 were given#  These 4 parameters are treated as positional parameters because of a missing parameter name city and the Job,python interpreter, but the person () function accepts only 2 positional arguments # person (' Jack ', 24,  ' Beijing ',  ' enginner ') #  if a variable parameter is already in the function definition, then the named keyword argument that follows will no longer require a special delimiter * Def person (name, age, *args, city, job):     print (Name, age, args,  city, job) person ("Yel", 23,  ' a ',  ' B ',  ' C ',  city= ' Chongqing ',  job= ' Eng ') The #  named keyword parameter can have a default value, which simplifies calling Def person (name, age, *, city= ' Beijing ',  job):     print (name, age, city, job) #  because the named keyword parameter city has a default value, when called, does not pass in the city parameter person (' Jack ',  24, job= ' Python ') #  when using the named keyword parameter, it is important to note that if there is no mutable parameter, you must add a * as a special delimiter #  if missing *, The Python interpreter will not recognize positional parameters and named keyword Parameters Def person (name, age, city, job):     #  missing *,city  and job are considered positional parameters     pass 


Python---function---named keyword parameter

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.