What is the purpose and use of **kwargs?

前端 未结 13 2253
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:59

What are the uses for **kwargs in Python?

I know you can do an objects.filter on a table and pass in a **kwargs argument. &nbs

13条回答
  •  故里飘歌
    2020-11-21 05:33

    This is the simple example to understand about python unpacking,

    >>> def f(*args, **kwargs):
    ...    print 'args', args, 'kwargs', kwargs
    

    eg1:

    >>>f(1, 2)
    >>> args (1,2) kwargs {} #args return parameter without reference as a tuple
    >>>f(a = 1, b = 2)
    >>> args () kwargs {'a': 1, 'b': 2} #args is empty tuple and kwargs return parameter with reference as a dictionary
    

提交回复
热议问题