Normal arguments vs. keyword arguments

后端 未结 10 846
一个人的身影
一个人的身影 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:45

    I'm surprised that no one seems to have pointed out that one can pass a dictionary of keyed argument parameters, that satisfy the formal parameters, like so.

    >>> def func(a='a', b='b', c='c', **kwargs):
    ...    print 'a:%s, b:%s, c:%s' % (a, b, c)
    ... 
    >>> func()
    a:a, b:b, c:c
    >>> func(**{'a' : 'z', 'b':'q', 'c':'v'})
    a:z, b:q, c:v
    >>> 
    

提交回复
热议问题