I was browsing through this file of code and then I found this class:
class StreamPlaylistEntry(BasePlaylistEntry):
def __init__(self, playlist, url, title, *, destination=None, **meta):
super().__init__()
I know that an asterisk in front of a parameter means it's a list of an arbitrary number of arguments, but what does the asterisk by itself mean?
It means that parameter(s) that comes after * are keyword only parameters.
The asterisk (star) operator is used in Python with more than one meaning attached to it. For numeric data types, * is used as multiplication operator >>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j.
Python has *args which allow us to pass the variable number of non keyword arguments to function. In the function, we should use an asterisk * before the parameter name to pass variable length arguments.
Here single asterisk( * ) is also used in *args. It is used to pass a variable number of arguments to a function, it is mostly used to pass a non-key argument and variable-length argument list.
It means all arguments afterwards are keyword-only. As said in the official glossary under the word parameter:
keyword-only: specifies an argument that can be supplied only by keyword. Keyword-only parameters can be defined by including a single var-positional parameter or bare
*
in the parameter list of the function definition before them, for example kw_only1 and kw_only2 in the following:def func(arg, *, kw_only1, kw_only2): ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With