Meaning of * in the function arguments list

Peter Otten __peter__ at web.de
Wed Oct 29 10:13:12 CET 2014


ast wrote:

> Consider the following to_bytes method from integer class:
> 
> int.to_bytes(length, byteorder, *, signed=False)
> 
> What doest the '*' in the arguments list means ?

A bare * indicates that the arguments that follow it are keyword-only:

>>> def f(a, b=2, *, c=3):
...     print("a = {}, b = {}, c = {}".format(a, b, c))
... 
>>> f(10)
a = 10, b = 2, c = 3
>>> f(10, 20)
a = 10, b = 20, c = 3
>>> f(10, 20, 30)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes from 1 to 2 positional arguments but 3 were given
>>> f(10, 20, c=30)
a = 10, b = 20, c = 30





More information about the Python-list mailing list