Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does * mean as a parameter in python?

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?

like image 529
Retronix Avatar asked Jul 16 '18 13:07

Retronix


People also ask

What does * mean in parameter list in Python?

It means that parameter(s) that comes after * are keyword only parameters.

What does * represent in Python?

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.

What is * in Python function argument?

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.

What does asterisk mean in Python 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.


1 Answers

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): ...
    
like image 68
Graipher Avatar answered Oct 19 '22 23:10

Graipher