DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

**kwargs in Python

Buy Me a Coffee

*Memo:

  • My post explains the unpacking with an assignment statement (1).
  • My post explains the unpacking with an assignment statement (2).
  • My post explains the unpacking with a for statement (1).
  • My post explains the unpacking with a for statement (2).
  • My post explains the iterable unpacking with * and a function (1).
  • My post explains the iterable unpacking with * and a function (2).
  • My post explains the dictionary unpacking with ** within a dictionary and function.
  • My post explains *args.

**kwargs can be used in a function as shown below:

*Memo:

  • A **kwargs is the dictionary parameter which can flexibly accept zero or more keyward arguments so the type is dict.
  • A **kwargs is called a var-keyword parameter.
  • A **kwargs is the optional parameter with the immutable default value {} so the default value {} cannot be changed with =.
  • Only one **kwargs can be used in a function definition.
  • A **kwargs must be the last parameter.
  • A **kwargs can be other names like **teachers, **students, etc.
  • One or more **dictionaries can be used within a dictionary and as the arguments within a function call excluding print() to unpack them into the one or more parameters including **kwargs but excluding *args within a function definition and used within a dictionary to unpack dictionaries:
    • One or more **dictionaries can be used with **kwargs but not with print() and *args.
def func(**kwargs): pass
def func(A, **kwargs): pass
def func(A='B', **kwargs): pass
# No error

def func(**kwargs, **kwargs): pass
# SyntaxError: * argument may appear only once

def func(**kwargs, A): pass
def func(**kwargs, A='B'): pass
# SyntaxError: arguments cannot follow var-keyword argument

def func(**kwargs='ABC'): pass
# SyntaxError: var-keyword argument cannot have default value
Enter fullscreen mode Exit fullscreen mode

<func(**kwargs) with keyword arguments>:

def func(**kwargs):
    print(kwargs)
    print({0:1, **kwargs, 2:3})

func()
# {}
# {0: 1, 2: 3}

func(A='B', C='D')
func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
# {'A': 'B', 'C': 'D'}
# {0: 1, 'A': 'B', 'C': 'D', 2: 3}
Enter fullscreen mode Exit fullscreen mode

<func(A, **kwargs) with keyword arguments>:

def func(A='b', **kwargs):
    print(A, kwargs)
    print(A, {0:1, **kwargs, 2:3})

func()
# b {}
# b {0: 1, 2: 3}

func(A='B', C='D')
func(**{'A':'B', 'C':'D'})
func(**{'A':'B'}, **{'C':'D'})
func(A='B', **{'C':'D'})
func(**{'A':'B'}, C='D')
# B {'C': 'D'}
# B {0: 1, 'C': 'D', 2: 3}
Enter fullscreen mode Exit fullscreen mode

<func(**kwargs) with positional arguments>:

def func(**kwargs):
    print(kwargs)
    print(**kwargs)

func()
# {}
# Nothing

func(0, 1, 2, 3, 4, 5)
func(*[0, 1, 2, 3], *[4, 5])                       # list
func(*(0, 1, 2, 3), *(4, 5))                       # tuple
func(*{0, 1, 2, 3}, *{4, 5})                       # set
func(*frozenset({0, 1, 2, 3}), *frozenset({4, 5})) # frozenset
func(0, 2, 4, 6, 8, 10)
func(*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11})         # dict
func(*{0:1, 2:3, 4:5, 6:7}.keys(),                 # dict.keys()
     *{8:9, 10:11}.keys())
func(*iter([0, 1, 2, 3]), *iter([4, 5]))           # iterator
func(*range(4), *range(4, 6))                      # range
func(1, 3, 5, 7, 9, 11)
func(*{0:1, 2:3, 4:5, 6:7}.values(),               # dict.values()
     *{8:9, 10:11}.values())
func((0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11))
func(*{0:1, 2:3, 4:5, 6:7}.items(),                # dict.items()
     *{8:9, 10:11}.items())
func('0', '1', '2', '3', '4', '5')
func(*'0123', *'45')                               # str
func(48, 49, 50, 51, 52, 53)
func(*b'0123', *b'45')                             # bytes
func(*bytearray(b'0123'), *bytearray(b'45'))       # bytearray
func(0, 1, 50, 3, 4, 5)
func(*[0, 1], *b'2', *{3, 4, 5})
# TypeError: func() takes 0 positional arguments but 6 were given
Enter fullscreen mode Exit fullscreen mode

Top comments (0)