DEV Community

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

Posted on

*args 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 **kwargs.

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

*Memo:

  • A *args is the tuple parameter which can flexibly accept zero or more positional arguments so the type is tuple.
  • A *args is called a var-positional parameter.
  • A *args is the optional parameter with the immutable default value () so the default value () cannot be changed with =.
  • Only one *args can be used in a function definition.
  • All the parameters after *args are keyword-only parameters so keyword arguments must be set to them.
  • *args can be other names like *teachers, *students, etc.
  • One or more *iterables can be used as the arguments within a function call including print() to unpack them into the one or more parameters including *args but excluding **kwargs within a function definition and used within a list, tuple or set to unpack iterables:
    • One or more *iterables can be used with print() and *args but not with **kwargs.
def func(*args): pass def func(A, *args, C): pass def func(A='B', *args, C='D'): pass # No error  def func(*args1, *args2): pass # SyntaxError: * argument may appear only once  def func(*args='ABC'): pass # SyntaxError: var-positional argument cannot have default value 
Enter fullscreen mode Exit fullscreen mode

<func(*args) with positional arguments>:

def func(*args): print(args) print(*args) print(['A', 'B', *args, 'C', 'D']) func() # () # Nothing # ['A', 'B', 'C', 'D']  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(*iter([0, 1, 2, 3]), *iter([4, 5])) # iterator func(*range(4), *range(4, 6)) # range # (0, 1, 2, 3, 4, 5) # 0 1 2 3 4 5 # ['A', 'B', 0, 1, 2, 3, 4, 5, 'C', 'D']  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()) # (0, 2, 4, 6, 8, 10) # 0 2 4 6 8 10 # ['A', 'B', 0, 2, 4, 6, 8, 10, 'C', 'D']  func(1, 3, 5, 7, 9, 11) func(*{0:1, 2:3, 4:5, 6:7}.values(), # dict.values()  *{8:9, 10:11}.values()) # (1, 3, 5, 7, 9, 11) # 1 3 5 7 9 11 # ['A', 'B', 1, 3, 5, 7, 9, 11, 'C', 'D']  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()) # ((0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)) # (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11) # ['A', 'B', (0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), 'C', 'D']  func('0', '1', '2', '3', '4', '5') func(*'0123', *'45') # str # ('0', '1', '2', '3', '4', '5') # 0 1 2 3 4 5 # ['A', 'B', '0', '1', '2', '3', '4', '5', 'C', 'D']  func(48, 49, 50, 51, 52, 53) func(*b'0123', *b'45') # bytes func(*bytearray(b'0123'), *bytearray(b'45')) # bytearray # (48, 49, 50, 51, 52, 53) # 48 49 50 51 52 53 # ['A', 'B', 48, 49, 50, 51, 52, 53, 'C', 'D']  func(0, 1, 50, 3, 4, 5) func(*[0, 1], *b'2', *{3, 4, 5}) # list # bytes # set # (0, 1, 50, 3, 4, 5) # 0 1 50 3 4 5 # ['A', 'B', 0, 1, 50, 3, 4, 5, 'C', 'D'] 
Enter fullscreen mode Exit fullscreen mode

<func(p1, p2, *args) with positional arguments>:

def func(p1='a', p2='b', *args): print(p1, p2, args) print(p1, p2, *args) print(p1, p2, ['A', 'B', *args, 'C', 'D']) func() # a b () # a b # a b ['A', 'B', 'C', 'D']  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(*iter([0, 1, 2, 3]), *iter([4, 5])) # iterator func(*range(4), *range(4, 6)) # range # 0 1 (2, 3, 4, 5) # 0 1 2 3 4 5 # 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']  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()) # 0 2 (4, 6, 8, 10) # 0 2 4 6 8 10 # 0 2 ['A', 'B', 4, 6, 8, 10, 'C', 'D']  func(1, 3, 5, 7, 9, 11) func(*{0:1, 2:3, 4:5, 6:7}.values(), # dict.values()  *{8:9, 10:11}.values()) # 1 3 (5, 7, 9, 11) # 1 3 5 7 9 11 # 1 3 ['A', 'B', 5, 7, 9, 11, 'C', 'D']  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()) # (0, 1) (2, 3) ((4, 5), (6, 7), (8, 9), (10, 11)) # (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11) # (0, 1) (2, 3) ['A', 'B', (4, 5), (6, 7), (8, 9), (10, 11), 'C', 'D']  func('0', '1', '2', '3', '4', '5') func(*'0123', *'45') # str # 0 1 ('2', '3', '4', '5') # 0 1 2 3 4 5 # 0 1 ['A', 'B', '2', '3', '4', '5', 'C', 'D']  func(48, 49, 50, 51, 52, 53) func(*b'0123', *b'45') # bytes func(*bytearray(b'0123'), *bytearray(b'45')) # bytearray # 48 49 (50, 51, 52, 53) # 48 49 50 51 52 53 # 48 49 ['A', 'B', 50, 51, 52, 53, 'C', 'D']  func(0, 1, 50, 3, 4, 5) # list # bytes # set func(*[0, 1], *b'2', *{3, 4, 5}) # 0 1 (50, 3, 4, 5) # 0 1 50 3 4 5 # 0 1 ['A', 'B', 50, 3, 4, 5, 'C', 'D'] 
Enter fullscreen mode Exit fullscreen mode

<func(*args) with keyword arguments>:

def func(*args): print(args) print(*args) func() # () # Nothing  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') # TypeError: func() got an unexpected keyword argument 'A' 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)