def <func_name>(<nondefault_args>): ... # E.g. `def func(x, y): ...`.
def <func_name>(<default_args>): ... # E.g. `def func(x=0, y=0): ...`.
def <func_name>(<nondefault_args>, <default_args>): ... # E.g. `def func(x, y=0): ...`.
```
* **Function returns None if it doesn't encounter `'return <obj/exp>'` statement.**
* **Before modifying a global variable from within the function run `'global <var_name>'`.**
* **Default values are evaluated when function is first encountered in the scope. Any mutation of a mutable default value will persist between invocations!**
<div><h2id="function"><ahref="#function"name="function">#</a>Function</h2><p><strong>Independent block of code that returns a value when called.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><<spanclass="hljs-title">func_name</span>><spanclass="hljs-params">(<nondefault_args>)</span>:</span> ... <spanclass="hljs-comment"># E.g. `def func(x, y): ...`.</span>
<spanclass="hljs-function"><spanclass="hljs-keyword">def</span><<spanclass="hljs-title">func_name</span>><spanclass="hljs-params">(<default_args>)</span>:</span> ... <spanclass="hljs-comment"># E.g. `def func(x=0, y=0): ...`.</span>
<spanclass="hljs-function"><spanclass="hljs-keyword">def</span><<spanclass="hljs-title">func_name</span>><spanclass="hljs-params">(<nondefault_args>, <default_args>)</span>:</span> ... <spanclass="hljs-comment"># E.g. `def func(x, y=0): ...`.</span>
</code></pre></div>
<ul>
<li><strong>Default values are evaluated when function is first encountered in the scope.</strong></li>
<li><strong>Any mutation of a mutable default value will persist between invocations!</strong></li>
<li><strong>Function returns None if it doesn't encounter <codeclass="python hljs"><spanclass="hljs-string">'return <obj/exp>'</span></code> statement.</strong></li>
<li><strong>Before modifying a global variable from within the function run <codeclass="python hljs"><spanclass="hljs-string">'global <var_name>'</span></code>.</strong></li>
<li><strong>Default values are evaluated when function is first encountered in the scope. Any mutation of a mutable default value will persist between invocations!</strong></li>
</ul>
<div><h2id="splatoperator"><ahref="#splatoperator"name="splatoperator">#</a>Splat Operator</h2><div><h3id="insidefunctioncall-1">Inside Function Call</h3><p><strong>Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.</strong></p><pre><codeclass="python language-python hljs">args = (<spanclass="hljs-number">1</span>,<spanclass="hljs-number">2</span>)
<div><h3id="functioncall">Function Call</h3><pre><codeclass="python language-python hljs"><obj> = <function>(<positional_args>) <spanclass="hljs-comment"># E.g. `func(0, 0)`.</span>
<obj> = <function>(<keyword_args>) <spanclass="hljs-comment"># E.g. `func(x=0, y=0)`.</span>
<obj> = <function>(<positional_args>, <keyword_args>) <spanclass="hljs-comment"># E.g. `func(0, y=0)`.</span>
</code></pre></div>
<div><h2id="splatoperator"><ahref="#splatoperator"name="splatoperator">#</a>Splat Operator</h2><p><strong>Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.</strong></p><pre><codeclass="python language-python hljs">args, kwargs = (<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>), {<spanclass="hljs-string">'z'</span>: <spanclass="hljs-number">3</span>}
func(*args, **kwargs)
</code></pre></div>
<div><h4id="isthesameas">Is the same as:</h4><pre><codeclass="python language-python hljs">func(<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>, x=<spanclass="hljs-number">3</span>, y=<spanclass="hljs-number">4</span>, z=<spanclass="hljs-number">5</span>)
<div><h4id="isthesameas">Is the same as:</h4><pre><codeclass="python language-python hljs">func(<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>, z=<spanclass="hljs-number">3</span>)
</code></pre></div>
<div><h3id="insidefunctiondefinition-1">Inside Function Definition</h3><p><strong>Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">add</span><spanclass="hljs-params">(*a)</span>:</span>
<spanclass="hljs-keyword">return</span> sum(a)
<div><h3id="insidefunctiondefinition">Inside Function Definition</h3><p><strong>Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">add</span><spanclass="hljs-params">(*a)</span>:</span>
<div><h4id="allowedcompositionsofargumentsinsidefunctiondefinitionandthewaystheycanbecalled">Allowed compositions of arguments inside function definition and the ways they can be called:</h4><pre><codeclass="python language-python hljs">┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━┓
<pre><codeclass="python language-python hljs">head, *body, tail = <coll.><spanclass="hljs-comment"># Head or tail can be omitted.</span>
<pre><codeclass="python language-python hljs">head, *body, tail = <collection><spanclass="hljs-comment"># Head or tail can be omitted.</span>
</code></pre>
<div><h2id="inline"><ahref="#inline"name="inline">#</a>Inline</h2><div><h3id="lambda">Lambda</h3><pre><codeclass="python language-python hljs"><func> = <spanclass="hljs-keyword">lambda</span>: <return_value><spanclass="hljs-comment"># A single statement function.</span>
<func> = <spanclass="hljs-keyword">lambda</span><arg_1>, <arg_2>: <return_value><spanclass="hljs-comment"># Also allows default arguments.</span>
@ -683,22 +676,30 @@ func(*args, **kwargs)
<div><h3id="conditionalexpression">Conditional Expression</h3><pre><codeclass="python language-python hljs"><obj> = <exp><spanclass="hljs-keyword">if</span><condition><spanclass="hljs-keyword">else</span><exp><spanclass="hljs-comment"># Only one expression is evaluated.</span>
<div><h3id="andor">And, Or</h3><pre><codeclass="python language-python hljs"><obj> = <exp><spanclass="hljs-keyword">and</span><exp> [<spanclass="hljs-keyword">and</span> ...] <spanclass="hljs-comment"># Returns first false or last operand.</span>
<obj> = <exp><spanclass="hljs-keyword">or</span><exp> [<spanclass="hljs-keyword">or</span> ...] <spanclass="hljs-comment"># Returns first true or last operand.</span>
</code></pre></div>
<div><h3id="walrusoperator">Walrus Operator</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>[i <spanclass="hljs-keyword">for</span> a <spanclass="hljs-keyword">in</span><spanclass="hljs-string">'0123'</span><spanclass="hljs-keyword">if</span> (i := int(a)) ><spanclass="hljs-number">0</span>] <spanclass="hljs-comment"># Assigns to variable mid-sentence.</span>
Point = namedtuple(<spanclass="hljs-string">'Point'</span>, <spanclass="hljs-string">'x y'</span>) <spanclass="hljs-comment"># Creates a tuple's subclass.</span>
Point = namedtuple(<spanclass="hljs-string">'Point'</span>, <spanclass="hljs-string">'x y'</span>) <spanclass="hljs-comment"># Creates tuple's subclass.</span>
point = Point(<spanclass="hljs-number">0</span>, <spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Returns its instance.</span>
Direction = Enum(<spanclass="hljs-string">'Direction'</span>, <spanclass="hljs-string">'N E S W'</span>) <spanclass="hljs-comment"># Creates an enum.</span>
Direction = Enum(<spanclass="hljs-string">'Direction'</span>, <spanclass="hljs-string">'N E S W'</span>) <spanclass="hljs-comment"># Creates Enum's subclass.</span>
direction = Direction.N <spanclass="hljs-comment"># Returns its member.</span>
Player = make_dataclass(<spanclass="hljs-string">'Player'</span>, [<spanclass="hljs-string">'loc'</span>, <spanclass="hljs-string">'dir'</span>]) <spanclass="hljs-comment"># Creates a class.</span>
player = Player(point, direction) <spanclass="hljs-comment"># Returns its instance.</span>
</code></pre>
</code></pre></div>
<div><h2id="imports"><ahref="#imports"name="imports">#</a>Imports</h2><p><strong>Mechanism that makes code in one file available to another file.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span><module><spanclass="hljs-comment"># Imports a built-in or '<module>.py'.</span>
<spanclass="hljs-keyword">import</span><package><spanclass="hljs-comment"># Imports a built-in or '<package>/__init__.py'.</span>
<spanclass="hljs-keyword">import</span><package>.<module><spanclass="hljs-comment"># Imports a built-in or '<package>/<module>.py'.</span>
@ -2944,7 +2945,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active