*
is the multiplication operator. All Python sequences support multiplication. See the Sequence Types documentation:
s * n
, n * s
n shallow copies of s concatenated
Note that the copies are shallow; any nested mutable types are not recursively copied too. This can lead to suprising results:
>>> nested = [[None]] * 5
>>> nested
[[None], [None], [None], [None], [None]]
>>> nested[0].append(42)
>>> nested
[[None, 42], [None, 42], [None, 42], [None, 42], [None, 42]]
There is just one nested [None]
list, referenced 5 times, not 5 separate list objects.
The *args
and **kw
variable argument syntax only has meaning in a function definition or in a call (so callable_object(<arguments>)
). It doesn't apply here at all. See What does ** (double star) and * (star) do for parameters? for more detail on that syntax.
Sequence types overload the *
operator through the object.__mul__()
and object.__rmul__()
methods (when being the left or right operand in the expression), see Emulating container types for documentation on what hooks sequence types typically implement.