Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The single star <code>*</code> unpacks the sequence/collection into positional arguments, so you can do this:</p> <pre><code>def sum(a, b): return a + b values = (1, 2) s = sum(*values) </code></pre> <p>This will unpack the tuple so that it actually executes as:</p> <pre><code>s = sum(1, 2) </code></pre> <p>The double star <code>**</code> does the same, only using a dictionary and thus named arguments:</p> <pre><code>values = { 'a': 1, 'b': 2 } s = sum(**values) </code></pre> <p>You can also combine:</p> <pre><code>def sum(a, b, c, d): return a + b + c + d values1 = (1, 2) values2 = { 'c': 10, 'd': 15 } s = sum(*values1, **values2) </code></pre> <p>will execute as: </p> <pre><code>s = sum(1, 2, c=10, d=15) </code></pre> <p>Also see section <a href="http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists" rel="noreferrer">4.7.4 - Unpacking Argument Lists</a> of the Python documentation.</p> <hr> <p>Additionally you can define functions to take <code>*x</code> and <code>**y</code> arguments, this allows a function to accept any number of positional and/or named arguments that aren't specifically named in the declaration.</p> <p>Example:</p> <pre><code>def sum(*values): s = 0 for v in values: s = s + v return s s = sum(1, 2, 3, 4, 5) </code></pre> <p>or with <code>**</code>:</p> <pre><code>def get_a(**values): return values['a'] s = get_a(a=1, b=2) # returns 1 </code></pre> <p>this can allow you to specify a large number of optional parameters without having to declare them.</p> <p>And again, you can combine:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload