Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In Python there are several types of arguments:</p> <ul> <li>positional and keyword</li> <li>named and arbitrary</li> </ul> <p>The function argument in Python is an attribution operation, that is, arguments are assigned to variables in the function local namespace.</p> <p>If you have a declaration like this:</p> <pre><code>def some_func(pos_arg1, pos_arg2, kw_arg1=1, kw_arg2='test'): print "postional arg 1 =", pos_arg1 print "postional arg 2 =", pos_arg2 print "keyword arg 1 =", kw_arg1 print "keyword arg 2 =", kw_arg2 </code></pre> <p>Positional arguments are mandatory and will be assigned in the given order, but keyword arguments are optional and can be called in any order - when omitted, named keyword arguments assume the declared default values (1 and 'test' in the example). So far:</p> <pre><code>&gt;&gt;&gt; some_func(1) # positional arguments are mandatory Traceback (most recent call last): File "&lt;interactive input&gt;", line 1, in &lt;module&gt; TypeError: some_func() takes at least 2 arguments (1 given) &gt;&gt;&gt; some_func(1, 2) # this is ok postional arg 1 = 1 postional arg 2 = 2 keyword arg 1 = 1 keyword arg 2 = test &gt;&gt;&gt; some_func(1, 2, 3) # this is also ok, keyword args may work like positional postional arg 1 = 1 postional arg 2 = 2 keyword arg 1 = 3 keyword arg 2 = test &gt;&gt;&gt; some_func(1, 2, 3, 4) # this is also ok, keyword args may work like positional postional arg 1 = 1 postional arg 2 = 2 keyword arg 1 = 3 keyword arg 2 = 4 &gt;&gt;&gt; some_func(1, 2, kw_arg2=3) # kyword arguments may be given in any order postional arg 1 = 1 postional arg 2 = 2 keyword arg 1 = 1 keyword arg 2 = 3 </code></pre> <p>There is a problem with undeclared arguments:</p> <pre><code>&gt;&gt;&gt; some_func(1, 2, 3, 4, 5) Traceback (most recent call last): File "&lt;interactive input&gt;", line 1, in &lt;module&gt; TypeError: some_func() takes at most 4 arguments (5 given) </code></pre> <p>But you can have an arbitrary number of arguments in using the special form <code>*</code> and <code>**</code>:</p> <pre><code>&gt;&gt;&gt; def some_func(pos_arg1, pos_arg2, *args, **kw_args): ... print "postional arg 1 =", pos_arg1 ... print "postional arg 2 =", pos_arg2 ... print "other positional orgs =", args ... print "other keyword args =", kw_args ... &gt;&gt;&gt; some_func(1, 2, 3, 4, 5) # any number of arguments postional arg 1 = 1 postional arg 2 = 2 other positional orgs = (3, 4, 5) other keyword args = {} &gt;&gt;&gt; some_func(1, 2, a=3, x=4, y=5) # * and ** are optional postional arg 1 = 1 postional arg 2 = 2 other positional orgs = () other keyword args = {'a': 3, 'x': 4, 'y': 5} &gt;&gt;&gt; some_func(1, 2, 'banana', 'orange', 'apple', a=3, x=4, y=5) postional arg 1 = 1 postional arg 2 = 2 other positional orgs = ('banana', 'orange', 'apple') other keyword args = {'a': 3, 'x': 4, 'y': 5} &gt;&gt;&gt; </code></pre> <p>The <code>*</code> argument will be available as a tuple of positional arguments, and <code>**</code> will be a dict of keyword arguments.</p> <p>You can mix everything together but there is a rule: all keyword arguments have to be declared after positional arguments, and all the arbitrary must be after named ones.</p>
 

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