Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a single * without identifier mean in the Python3 functions? [duplicate]

Tags:

What does a single * without identifier mean in the Python function arguments? Here is an example that works with Python3.2:

class Shape:     def __init__(self, *, shapename, **kwds):         self.shapename = shapename         super().__init__(**kwds) 

For me the star after the self is strange.

I have found it here (from row 46): http://code.activestate.com/recipes/577720-how-to-use-super-effectively/

like image 375

People also ask

What does single * mean in Python?

Single asterisk as used in function declaration allows variable number of arguments passed from calling environment. Inside the function it behaves as a tuple. >>> def function(*arg): print (type(arg)) for i in arg: print (i) >>> function(1,2,3) <class 'tuple'> 1 2 3. Malhar Lathkar.

What does * mean in Python 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 is a single argument in Python?

The terms parameter and argument can be used for the same thing: information that are passed into a function. From a function's perspective: A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that are sent to the function when it is called.

Which are the advantages of functions in Python *?

The advantages of using functions are: Reducing duplication of code. Decomposing complex problems into simpler pieces. Improving clarity of the code.


1 Answers

The lone * indicates that all following arguments are keyword-only arguments, that is, they can only be provided using their name, not as positional argument.

See PEP 3102 for further details.

like image 55
Sven Marnach Avatar answered Oct 12 '22 11:10

Sven Marnach