Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the star and doublestar operator mean in a function call?

What does the * operator mean in Python, such as in code like zip(*x) or f(**k)?

  1. How is it handled internally in the interpreter?
  2. Does it affect performance at all? Is it fast or slow?
  3. When is it useful and when is it not?
  4. Should it be used in a function declaration or in a call?
like image 617
psihodelia Avatar asked May 27 '10 14:05

psihodelia


People also ask

What is the star operator?

The asterisk (star) operator is used in Python with more than one meaning attached to it. Single asterisk as used in function declaration allows variable number of arguments passed from calling environment. Inside the function it behaves as a tuple.

What does the * operator means in def Myfunction (* args ):?

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.

What does * VAR mean in Python?

The var() function is part of the standard library in Python and is used to get an object's _dict_ attribute. The returned _dict_ attribute contains the changeable attributes of the object. This means that when we update the attribute list of an object, the var() function will return the updated dictionary.

What * line means in Python?

The * is being used to grab additional returns from the split statement. Show activity on this post. In Python, it's called the unpacking operator, it was introduced in Python 3 (this specific operation).


2 Answers

The single star * unpacks the sequence/collection into positional arguments, so you can do this:

def sum(a, b):     return a + b  values = (1, 2)  s = sum(*values) 

This will unpack the tuple so that it actually executes as:

s = sum(1, 2) 

The double star ** does the same, only using a dictionary and thus named arguments:

values = { 'a': 1, 'b': 2 } s = sum(**values) 

You can also combine:

def sum(a, b, c, d):     return a + b + c + d  values1 = (1, 2) values2 = { 'c': 10, 'd': 15 } s = sum(*values1, **values2) 

will execute as:

s = sum(1, 2, c=10, d=15) 

Also see section 4.7.4 - Unpacking Argument Lists of the Python documentation.


Additionally you can define functions to take *x and **y arguments, this allows a function to accept any number of positional and/or named arguments that aren't specifically named in the declaration.

Example:

def sum(*values):     s = 0     for v in values:         s = s + v     return s  s = sum(1, 2, 3, 4, 5) 

or with **:

def get_a(**values):     return values['a']  s = get_a(a=1, b=2)      # returns 1 

this can allow you to specify a large number of optional parameters without having to declare them.

And again, you can combine:

def sum(*values, **options):     s = 0     for i in values:         s = s + i     if "neg" in options:         if options["neg"]:             s = -s     return s  s = sum(1, 2, 3, 4, 5)            # returns 15 s = sum(1, 2, 3, 4, 5, neg=True)  # returns -15 s = sum(1, 2, 3, 4, 5, neg=False) # returns 15 
like image 80
Lasse V. Karlsen Avatar answered Sep 20 '22 18:09

Lasse V. Karlsen


One small point: these are not operators. Operators are used in expressions to create new values from existing values (1+2 becomes 3, for example. The * and ** here are part of the syntax of function declarations and calls.

like image 28
Ned Batchelder Avatar answered Sep 17 '22 18:09

Ned Batchelder