Normal arguments vs. keyword arguments

后端 未结 10 844
一个人的身影
一个人的身影 2020-11-22 02:35

How are \"keyword arguments\" different from regular arguments? Can\'t all arguments be passed as name=value instead of using positional syntax?

10条回答
  •  春和景丽
    2020-11-22 02:49

    I was looking for an example that had default kwargs using type annotation:

    def test_var_kwarg(a: str, b: str='B', c: str='', **kwargs) -> str:
         return ' '.join([a, b, c, str(kwargs)])
    

    example:

    >>> print(test_var_kwarg('A', c='okay'))
    A B okay {}
    >>> d = {'f': 'F', 'g': 'G'}
    >>> print(test_var_kwarg('a', c='c', b='b', **d))
    a b c {'f': 'F', 'g': 'G'}
    >>> print(test_var_kwarg('a', 'b', 'c'))
    a b c {}
    

提交回复
热议问题