DEV Community

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

Posted on • Edited on

`*` iterable unpacking for Python function

Buy Me a Coffee

*Memos:

  • My post explains parameters and arguments.
  • My post explains iterable unpacking in variable assignment.
  • My post explains * for iterable unpacking in variable assignment.
  • My post explains ** for dictionary unpacking.
  • My post explains *args and **kwargs in function.

You can use * for a function parameter and argument for iterable unpacking as shown below:
*Memos:

  • A * is an iterable unpacking operator.
  • A function can have only one *parameter which is *args conventionally. *A *parameter is a var-positional parameter.
  • A *parameter can store zero or more values as a tuple.
  • A *parameter cannot have a default value.
  • Zero or more *iterables can be used as the arguments passed to the zero or more parameters including a *args and **kwargs.
def func(*args): pass
def func(*mylist): pass
def func(p, *args): pass
def func(p="param", *args): pass
def func(*args, p): pass
def func(*args, p="param"): pass
def func(*args, **kwargs): pass
def func(p, *args, **kwargs): pass
def func(p="param", *args, **kwargs): pass
def func(*args, p, **kwargs): pass
def func(*args, p="param", **kwargs): pass
# No error

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

def func(**kwargs, *args): pass
# SyntaxError: arguments cannot follow var-keyword argument

def func(*args=["John"]): pass
def func(*args=*["John"]): pass
# SyntaxError: var-positional argument cannot have default value
Enter fullscreen mode Exit fullscreen mode
def func(*args):
    print(args, *args)

func()
func(*[])
func(*())
func(*{})
func(**{})
# () Nothing

func({})
# ({},) {}

func(0)
func(*[0])
func(*(0,))
# (0,) 0

func(0, 1)
func(*[0, 1])
func(*(0, 1))
# (0, 1) 0 1

func(0, 1, 2)
func(*[0, 1, 2])
func(*(0, 1, 2))
# (0, 1, 2) 0 1 2

func(0, 1, 2, 3)
func(*[0, 1, 2, 3])
func(*(0, 1, 2, 3))
# (0, 1, 2, 3) 0 1 2 3

func(0, 1, 2, 3, 4)
func(*[0, 1, 2, 3, 4])
func(*(0, 1, 2, 3, 4))
# (0, 1, 2, 3, 4) 0 1 2 3 4

func([])
# ([],) []

func([0])
# ([0],) [0]

func([0, 1])
# ([0, 1],) [0, 1]

func([0, 1, 2])
# ([0, 1, 2],) [0, 1, 2]

func([0, 1, 2, 3])
# ([0, 1, 2, 3],) [0, 1, 2, 3]

func([0, 1, 2, 3, 4])
# ([0, 1, 2, 3, 4],) [0, 1, 2, 3, 4]

func(())
# ((),) ()

func((0,))
# ((0,),) (0,)

func((0, 1))
# ((0, 1),) (0, 1)

func((0, 1, 2))
# ((0, 1, 2),) (0, 1, 2)

func((0, 1, 2, 3))
# ((0, 1, 2, 3),) (0, 1, 2, 3)

func((0, 1, 2, 3, 4))
# ((0, 1, 2, 3, 4),) (0, 1, 2, 3, 4)

func({"fname":"John", "lname":"Smith"})
# ({'fname': 'John', 'lname': 'Smith'},) {'fname': 'John', 'lname': 'Smith'}

func({"fname":"John"}, {"lname":"Smith"})
# ({'fname': 'John'}, {'lname': 'Smith'}) {'fname': 'John'} {'lname': 'Smith'}

func(*{"fname":"John", "lname":"Smith"})
func(*{"fname":"John"}, *{"lname":"Smith"})
# ('fname', 'lname') fname lname
Enter fullscreen mode Exit fullscreen mode
def func(p1="p1", p2="p2", *args, p3="p3"):
    print(p1, p2, args, p3)

func()
func(*[])
func(*())
func(*{})
func(**{})
# p1 p2 () p3

func({})
# {} p2 () p3

func(0)
func(*[0])
func(*(0,))
# 0 p2 () p3

func(0, 1)
func(*[0, 1])
func(*(0, 1))0 1 () p3
# 0 1 () p3

func(0, 1, 2)
func(*[0, 1, 2])
func(*(0, 1, 2))
# 0 1 (2,) p3

func(0, 1, 2, 3)
func(*[0, 1, 2, 3])
func(*(0, 1, 2, 3))
# 0 1 (2, 3) p3

func(0, 1, 2, 3, 4)
func(*[0, 1, 2, 3, 4])
func(*(0, 1, 2, 3, 4))
# 0 1 (2, 3, 4) p3

func([])
# [] p2 () p3

func([0])
# [0] p2 () p3

func([0, 1])
# [0, 1] p2 () p3

func([0, 1, 2])
# [0, 1, 2] p2 () p3

func([0, 1, 2, 3])
# [0, 1, 2, 3] p2 () p3

func([0, 1, 2, 3, 4])
# [0, 1, 2, 3, 4] p2 () p3

func(())
# () p2 () p3

func((0,))
# (0,) p2 () p3

func((0, 1))
# (0, 1) p2 () p3

func((0, 1, 2))
# (0, 1, 2) p2 () p3

func((0, 1, 2, 3))
# (0, 1, 2, 3) p2 () p3

func((0, 1, 2, 3, 4))
# (0, 1, 2, 3, 4) p2 () p3

func({"fname":"John", "lname":"Smith"})
# {'fname': 'John', 'lname': 'Smith'} p2 () p3

func({"fname":"John"}, {"lname":"Smith"})
# {'fname': 'John'} {'lname': 'Smith'} () p3

func(*{"fname":"John", "lname":"Smith"})
func(*{"fname":"John"}, *{"lname":"Smith"})
# fname lname () p3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)