Skip to main content
Remove example that was about something else. Illustrate "except formal parameters" instead of an inconsistent note. Fix details and grammar. Improve code style, flow, and clarity. Add result of Extended Iterable Unpacking but remove redundant example.
Source Link
wjandrea
  • 34.2k
  • 10
  • 71
  • 108

The *args and **kwargs is aare common idiomidioms to allow an arbitrary number of arguments to functions, as described in the section more on defining functions in the Python documentationtutorial.

The *args will give you all function parameterspositional arguments as a tuple:

def foo(*args):
    for a in args:
        print(a)        

foo(1)
# 1

foo(1, 2, 3)
# 1
# 2
# 3

The **kwargs will give you all keyword arguments except for those corresponding to a formal parameterkeyword arguments as a dictionary.:

def foo(kind, *args, bar=None, **kwargs):
   pass print(kind, args, bar, kwargs)

foo(123, 'a', 'b', apple='red')
# 123 ('a', 'b') None {'apple': 'red'}
def foo(a, b, c):
    print(a, b, c)

obj = {'b':10, 'c':'lee'}

foo(100, **obj)
# 100 10 lee
def foo(bar, lee):
    print(bar, lee)

lbaz = [1, 2]

foo(*l*baz)
# 1 2
first, *rest = [1, 2, 3, 4]
# first, *l,= last1
# rest = [1,2[2, 3, 4]

Also Python 3 adds a new semantic (refer PEP 3102):

def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass

For example the following works in python 3 but not python 2:

>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
  • A Python dict, semantically used for keyword argument passing, are arbitrarily ordered. However, in Python 3.6, keyword arguments are guaranteed to remember insertion order.
  • "The order of elements in **kwargs now corresponds to the order in which keyword arguments were passed to the function." - What’s New In Python 3.6
  • In fact, all dicts in CPython 3.6 will remember insertion order as an implementation detail, this becomes standard in Python 3.7.

A Python dict, semantically used for keyword argument passing, is arbitrarily ordered. However, in Python 3.6+, keyword arguments are guaranteed to remember insertion order. "The order of elements in **kwargs now corresponds to the order in which keyword arguments were passed to the function." - What’s New In Python 3.6. In fact, all dicts in CPython 3.6 will remember insertion order as an implementation detail, and this becomes standard in Python 3.7.

The *args and **kwargs is a common idiom to allow arbitrary number of arguments to functions as described in the section more on defining functions in the Python documentation.

The *args will give you all function parameters as a tuple:

def foo(*args):
    for a in args:
        print(a)        

foo(1)
# 1

foo(1,2,3)
# 1
# 2
# 3

The **kwargs will give you all keyword arguments except for those corresponding to a formal parameter as a dictionary.

def foo(kind, *args, **kwargs):
   pass
def foo(a, b, c):
    print(a, b, c)

obj = {'b':10, 'c':'lee'}

foo(100,**obj)
# 100 10 lee
def foo(bar, lee):
    print(bar, lee)

l = [1,2]

foo(*l)
# 1 2
first, *rest = [1,2,3,4]
first, *l, last = [1,2,3,4]

Also Python 3 adds new semantic (refer PEP 3102):

def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass

For example the following works in python 3 but not python 2:

>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
  • A Python dict, semantically used for keyword argument passing, are arbitrarily ordered. However, in Python 3.6, keyword arguments are guaranteed to remember insertion order.
  • "The order of elements in **kwargs now corresponds to the order in which keyword arguments were passed to the function." - What’s New In Python 3.6
  • In fact, all dicts in CPython 3.6 will remember insertion order as an implementation detail, this becomes standard in Python 3.7.

The *args and **kwargs are common idioms to allow an arbitrary number of arguments to functions, as described in the section more on defining functions in the Python tutorial.

The *args will give you all positional arguments as a tuple:

def foo(*args):
    for a in args:
        print(a)        

foo(1)
# 1

foo(1, 2, 3)
# 1
# 2
# 3

The **kwargs will give you all keyword arguments as a dictionary:

def foo(kind, *args, bar=None, **kwargs):
    print(kind, args, bar, kwargs)

foo(123, 'a', 'b', apple='red')
# 123 ('a', 'b') None {'apple': 'red'}
def foo(a, b, c):
    print(a, b, c)

obj = {'b':10, 'c':'lee'}

foo(100, **obj)
# 100 10 lee
def foo(bar, lee):
    print(bar, lee)

baz = [1, 2]

foo(*baz)
# 1 2
first, *rest = [1, 2, 3, 4]
# first = 1
# rest = [2, 3, 4]

Also Python 3 adds a new semantic (refer PEP 3102):

def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass

A Python dict, semantically used for keyword argument passing, is arbitrarily ordered. However, in Python 3.6+, keyword arguments are guaranteed to remember insertion order. "The order of elements in **kwargs now corresponds to the order in which keyword arguments were passed to the function." - What’s New In Python 3.6. In fact, all dicts in CPython 3.6 will remember insertion order as an implementation detail, and this becomes standard in Python 3.7.

added use cases
Source Link
voxobscuro
  • 2.2k
  • 1
  • 22
  • 48
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass
 
The following works in python 3 not python 2:

For example the following works in python 3 but not python 2:

>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass
 
The following works in python 3 not python 2:
>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass

For example the following works in python 3 but not python 2:

>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass 

The following works in python 3 not python 2:
>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
    pass 

The following works in python 3 not python 2:
>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]

>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
updated documentation links
Source Link
MattDMo
  • 103.3k
  • 21
  • 251
  • 239
Loading
corrected the output order based on 3.7 as per a now deleted comment and added response.
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
corrected the output order as per a now deleted comment
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
made the output as comments (like this it's more clear as before IMHO)
Source Link
Georgy
  • 14k
  • 7
  • 70
  • 80
Loading
Add another important example
Source Link
JakeTheSnake
  • 2.5k
  • 3
  • 17
  • 27
Loading
Change to python3 syntax
Source Link
JakeTheSnake
  • 2.5k
  • 3
  • 17
  • 27
Loading
Fixed wrong info.
Source Link
user2357112
  • 286.9k
  • 32
  • 492
  • 575
Loading
Added a link to the PEP for the new python3 semantic
Source Link
Loading
Added `single *` variant supported in Python3
Source Link
Loading
Fixed spelling
Source Link
Craig S. Anderson
  • 7.5k
  • 4
  • 37
  • 48
Loading
Improved formatting.
Source Link
Anonymous
  • 12.2k
  • 3
  • 44
  • 51
Loading
the args are a tuple, not a list
Source Link
Pat
  • 17k
  • 16
  • 101
  • 116
Loading
added 3 characters in body
Source Link
Alex Bitek
  • 6.6k
  • 5
  • 50
  • 78
Loading
update to 2013
Source Link
Kos
  • 72.8k
  • 28
  • 174
  • 239
Loading
Typos
Source Link
Jim Ferrans
  • 31.1k
  • 12
  • 59
  • 83
Loading
added 279 characters in body
Source Link
Peter Hoffmann
  • 59.2k
  • 15
  • 78
  • 60
Loading
added 13 characters in body
Source Link
Peter Hoffmann
  • 59.2k
  • 15
  • 78
  • 60
Loading
added 157 characters in body
Source Link
Peter Hoffmann
  • 59.2k
  • 15
  • 78
  • 60
Loading
added 50 characters in body
Source Link
Peter Hoffmann
  • 59.2k
  • 15
  • 78
  • 60
Loading
Source Link
Peter Hoffmann
  • 59.2k
  • 15
  • 78
  • 60
Loading