Day5 - 06 Function Parameters of Python Study Notes - Named Keyword Parameters

Introduction:
For keyword parameters, you can pass in any number of unrestricted keyword parameters when calling. As for which ones are passed in, you need to check the [keyword parameters defined in the function] inside the function. In the example, pass otherinfo check.
        >>> def person(name,age,**otherinfo):
        ... print('name:',name,'age:',age,'Other infomations:',otherinfo)
        ...
    for example, we want to check Whether to include sex and city parameters,
       >>> def person(name,age,**other):
        ... if 'sex' in other:
        ... pass
        ... if 'city' in other:
        ... pass
        ... print('name:',name,'age:',age,'other:',other)
        ...
        >>> person('kk',12,sex='M',city='BJ ')
        name:
        >>> person('kk',12,oo='aa')
        name: kk age: 12 other: {'oo': 'aa'}
    You can find that you can still pass in unlimited keyword arguments when calling oo, if you want to restrict the names of keyword arguments, you can use named keyword arguments.
 
Named Keyword Parameters Body:
    Still using this example, we limit the names of the keyword parameters to only accept sex and city as keyword parameters, defined as follows:
        >>> def person(name, age, *, sex, city): # Note the asterisk, detailed below.
        ... print('name:',name,'age:',age,city)
        ... 
        >>> person('kk',13,sex='M',city='BJ')
        name: kk age: 13 BJ
       
Use of named keyword arguments:
    Say this asterisk.
        Unlike keyword arguments, named keyword arguments require a special delimiter * , and arguments following * are treated as named keyword arguments. The parameter name must be passed in when calling, otherwise an error will be reported, which is different from positional parameters. When the parameter name is not specified, it will be regarded as a positional parameter by the function, but the above function has only two positional parameters, so an error will be reported.
        1. *, the following parameters are treated as named keyword parameters.
        2. If there is already a variadic parameter in the function definition, the special delimiter * is no longer required for named keyword arguments following the variadic parameter.
        3. Named keyword arguments can have default values ​​to simplify calling. Named keyword arguments with default values ​​can be passed without this argument when calling the function. Definition of default values ​​for named keyword arguments:
            def person(name, age, *,
            ……
Problems found when learning named keywords:
    Back to the example in the text, define the person function, which contains two named keyword parameters, sex and city.
         >>> def person(name, age, *, sex, city):  
        ... print('name:',name,'age:',age,city)
        ... 
        >>> person('kk', 13,sex='M',city='BJ')
        name: kk age: 13 BJ #In    
    this step, there is no problem. However, keyword parameters are extensions, so are named keyword parameters the same as keyword parameters, and can accept 0 parameters?
     
        >>> person('kk',13) #Only pass in the positional parameters name and age, no other parameters are passed
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError : person() missing 2 required keyword-only arguments: 'sex' and 'city'
    #Error reported, test again, is the named keyword parameter the same as the keyword parameter, can any number of parameters be passed in? (Is it arbitrary as long as the named keyword parameters are passed in?)
   
        >>> person('kk',13,sex='M',city='BJ',Phone=110,weight=100)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: person() got an unexpected keyword argument 'Phone' #An
    error is still reported. Strange discovery, in this way, named keyword arguments and positional arguments seem to be completely indistinguishable! All of these parameters are forced to be input, and it is impossible to lack one or more than one.
       
Why? ------ After you have learned the following parameter combinations, you will understand (post note).
-------------------------------------------------- ---------      
From Zhihu:
Since there are already positional parameters, is this named keyword parameter api design redundant? Positional parameters are required, so functionally, it can completely replace named keyword parameters. And also easy to understand. In addition to the need to write the key when calling, and the writing order can be changed, is there anything else? Is it because it makes the caller a little more flexible?
-------------------------------------------------- ---------      
def func1(a, *q , * , b , **w)
A function of any form can be called with the form func(*args, **kw), no matter how its parameters are defined. Where args is a list and kw is a dict.
For example, for the function func1, the following two methods can be used:
func1( 1 , *[2,3] , b=1, **{ 'w1':2 , 'w2 ':3 } )
or:
func1( *[1,2,3] , **{ 'b':1, 'w1':2 , 'w2':3 } )

Why is there a required positional parameter and a required named keyword parameter?
My understanding is that positional parameters are a required element in list and named keywords are a required element in dict.
Not much to say about the difference between list and dict

Author: Aunt Jiao
Link : https://www.zhihu.com/question/57726430/answer/302836571
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.
-------------------------------------------------- ---------  
Named keywords From the above example, there are obvious differences from positional parameters. Named keyword arguments are more flexible. However, it still seems a little dizzy now. In the follow-up study, I will see if I can fully understand the named keyword parameters.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325237584&siteId=291194637