# 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