<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Functions</id>
	<title>Functions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Functions"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Functions&amp;action=history"/>
	<updated>2026-09-30T04:55:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Functions&amp;diff=323&amp;oldid=prev</id>
		<title>Davrot at 16:09, 17 October 2025</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Functions&amp;diff=323&amp;oldid=prev"/>
		<updated>2025-10-17T16:09:31Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:09, 17 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== The goal ==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A function allows to separate a part of the code in a logical module that can be reused.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A function allows to separate a part of the code in a logical module that can be reused.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Functions&amp;diff=149&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot; == The goal == A function allows to separate a part of the code in a logical module that can be reused.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  &#039;&#039;&#039;Logic blocks need to be indented.​ Preferable with 4 spaces!&#039;&#039;&#039;  == [https://docs.python.org/3/tutorial/controlflow.html#defining-functions Most simple function] == These three functions are equivalent:&lt;syntaxhighlight lang=&quot;python&quot;&gt;def my_function():     pass  def my_function():     return  def my_func...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Functions&amp;diff=149&amp;oldid=prev"/>
		<updated>2025-10-15T13:17:12Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; == The goal == A function allows to separate a part of the code in a logical module that can be reused.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  &amp;#039;&amp;#039;&amp;#039;Logic blocks need to be indented.​ Preferable with 4 spaces!&amp;#039;&amp;#039;&amp;#039;  == [https://docs.python.org/3/tutorial/controlflow.html#defining-functions Most simple function] == These three functions are equivalent:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():     pass  def my_function():     return  def my_func...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== The goal ==&lt;br /&gt;
A function allows to separate a part of the code in a logical module that can be reused.&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Logic blocks need to be indented.​ Preferable with 4 spaces!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#defining-functions Most simple function] ==&lt;br /&gt;
These three functions are equivalent:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    return&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    return None&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;return leaves the current function call with the expression list (or None) as return value.&amp;lt;/blockquote&amp;gt;There is a return at the end, even if you don’t put it there.&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/reference/compound_stmts.html?highlight=funcdef#function-definitions def] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;funcdef                   ::=  [decorators] &amp;quot;def&amp;quot; funcname [type_params] &amp;quot;(&amp;quot; [parameter_list] &amp;quot;)&amp;quot;&lt;br /&gt;
                               [&amp;quot;-&amp;gt;&amp;quot; expression] &amp;quot;:&amp;quot; suite&lt;br /&gt;
decorators                ::=  decorator+&lt;br /&gt;
decorator                 ::=  &amp;quot;@&amp;quot; assignment_expression NEWLINE&lt;br /&gt;
parameter_list            ::=  defparameter (&amp;quot;,&amp;quot; defparameter)* &amp;quot;,&amp;quot; &amp;quot;/&amp;quot; [&amp;quot;,&amp;quot; [parameter_list_no_posonly]]&lt;br /&gt;
                                 | parameter_list_no_posonly&lt;br /&gt;
parameter_list_no_posonly ::=  defparameter (&amp;quot;,&amp;quot; defparameter)* [&amp;quot;,&amp;quot; [parameter_list_starargs]]&lt;br /&gt;
                               | parameter_list_starargs&lt;br /&gt;
parameter_list_starargs   ::=  &amp;quot;*&amp;quot; [parameter] (&amp;quot;,&amp;quot; defparameter)* [&amp;quot;,&amp;quot; [&amp;quot;**&amp;quot; parameter [&amp;quot;,&amp;quot;]]]&lt;br /&gt;
                               | &amp;quot;**&amp;quot; parameter [&amp;quot;,&amp;quot;]&lt;br /&gt;
parameter                 ::=  identifier [&amp;quot;:&amp;quot; expression]&lt;br /&gt;
defparameter              ::=  parameter [&amp;quot;=&amp;quot; expression]&lt;br /&gt;
funcname                  ::=  identifier&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/reference/simple_stmts.html#the-pass-statement pass] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;pass_stmt ::=  &amp;quot;pass&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/reference/simple_stmts.html#the-return-statement return] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;return_stmt ::=  &amp;quot;return&amp;quot; [expression_list]&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;return may only occur syntactically nested in a function definition, not within a nested class definition.&lt;br /&gt;
&lt;br /&gt;
If an expression list is present, it is evaluated, else None is substituted.&lt;br /&gt;
&lt;br /&gt;
return leaves the current function call with the expression list (or None) as return value.&lt;br /&gt;
&lt;br /&gt;
When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/reference/simple_stmts.html#return Return values​] ==&lt;br /&gt;
&lt;br /&gt;
=== No return value ===&lt;br /&gt;
As we stated above these functions all return None:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    return&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    return None&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== One return value ===&lt;br /&gt;
These functions return one value:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    return 1&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    return &amp;quot;Hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    return (1+1)/1&amp;lt;/syntaxhighlight&amp;gt;e.g. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    return 1&lt;br /&gt;
&lt;br /&gt;
print(my_function())&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Many return values ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    return 2, &amp;quot;A&amp;quot;, 79, 3.1314&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(my_function()) # -&amp;gt; (2, &amp;#039;A&amp;#039;, 79, 3.1314)&lt;br /&gt;
abcd = my_function() # -&amp;gt; (2, &amp;#039;A&amp;#039;, 79, 3.1314)&lt;br /&gt;
print(abcd) # -&amp;gt; (2, &amp;#039;A&amp;#039;, 79, 3.1314)&lt;br /&gt;
&lt;br /&gt;
a, b, c, d = my_function()&lt;br /&gt;
&lt;br /&gt;
print(a) # -&amp;gt; 2&lt;br /&gt;
print(b) # -&amp;gt; A&lt;br /&gt;
print(c) # -&amp;gt; 79&lt;br /&gt;
print(d) # -&amp;gt; 3.1314&lt;br /&gt;
&lt;br /&gt;
a, b, c = my_function() # ValueError: too many values to unpack (expected 3)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#defining-functions Functions are objects] ==&lt;br /&gt;
Functions are objects. Without &amp;#039;&amp;#039;&amp;#039;()&amp;#039;&amp;#039;&amp;#039; at the end, you interact with the function. With the &amp;#039;&amp;#039;&amp;#039;()&amp;#039;&amp;#039;&amp;#039; you use the function:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    return 1&lt;br /&gt;
&lt;br /&gt;
print(my_function)  # -&amp;gt; &amp;lt;function my_function at 0x7f956166a520&amp;gt;&lt;br /&gt;
print(my_function())  # -&amp;gt; 1&amp;lt;/syntaxhighlight&amp;gt;Interesting, but why do I need to know this? Good question, imaginary person! We can use functions as “parameters” via arguments for other functions:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function_0():&lt;br /&gt;
    return 3&lt;br /&gt;
&lt;br /&gt;
def my_function_1():&lt;br /&gt;
    return 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def my_function_2(func):&lt;br /&gt;
    return func() ** 2&lt;br /&gt;
&lt;br /&gt;
print(my_function_2(my_function_0))  # -&amp;gt; 9&lt;br /&gt;
print(my_function_2(my_function_1))  # -&amp;gt; 4&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#default-argument-values Default Argument Values] ==&lt;br /&gt;
We can define a default value for arguments:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function(a=2, b=7, c=5, d=8):&lt;br /&gt;
    return a + b**2 + c**3 + d**4&lt;br /&gt;
&lt;br /&gt;
print(my_function()) # -&amp;gt; 4272&amp;lt;/syntaxhighlight&amp;gt;We can (partially) overwrite the default values:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function(a=2, b=7, c=5, d=8):&lt;br /&gt;
    return a + b**2 + c**3 + d**4&lt;br /&gt;
&lt;br /&gt;
print(my_function(b=2,a=3)) # -&amp;gt; 4228&lt;br /&gt;
print(my_function(d=7))  # -&amp;gt; 2577&lt;br /&gt;
print(my_function(3,2)) # -&amp;gt; 4228&lt;br /&gt;
print(my_function(3,b=2))  # -&amp;gt; 4228&lt;br /&gt;
print(my_function(b=2,3)) # SyntaxError: positional argument follows keyword argument&amp;lt;/syntaxhighlight&amp;gt;In the case of &amp;#039;&amp;#039;&amp;#039;my_function(3,2)&amp;#039;&amp;#039;&amp;#039;, the default values are replaced in the order the arguments of the function are defined. a,b,c, and finally d. When using the keyword e.g. d. Then I can overwrite only selected arguments.&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#default-argument-values Important warning] – Mutable objects ==&lt;br /&gt;
&amp;lt;blockquote&amp;gt;Important warning: &amp;#039;&amp;#039;&amp;#039;The default value is evaluated only once&amp;#039;&amp;#039;&amp;#039;. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes.&amp;lt;/blockquote&amp;gt;Deadly:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def f(a, L=[]):&lt;br /&gt;
    L.append(a)&lt;br /&gt;
    return L&lt;br /&gt;
&lt;br /&gt;
print(f(1)) # -&amp;gt; [1]&lt;br /&gt;
print(f(2)) # -&amp;gt; [1, 2]&lt;br /&gt;
print(f(3)) # -&amp;gt; [1, 2, 3]&amp;lt;/syntaxhighlight&amp;gt;Correct:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def f(a, L=None):&lt;br /&gt;
    if L is None:&lt;br /&gt;
        L = []&lt;br /&gt;
    L.append(a)&lt;br /&gt;
    return L&lt;br /&gt;
&lt;br /&gt;
print(f(1))  # -&amp;gt; [1]&lt;br /&gt;
print(f(2))  # -&amp;gt; [2]&lt;br /&gt;
print(f(3))  # -&amp;gt; [3]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Mutable arguments don’t need return values ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def f(x):&lt;br /&gt;
    x.append(1)&lt;br /&gt;
&lt;br /&gt;
y = []&lt;br /&gt;
f(y)&lt;br /&gt;
print(y)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#special-parameters Arguments: Positional and Keywords] ==&lt;br /&gt;
&amp;lt;blockquote&amp;gt;A function definition may look like:&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):&lt;br /&gt;
      -----------    ----------     ----------&lt;br /&gt;
        |             |                  |&lt;br /&gt;
        |        Positional or keyword   |&lt;br /&gt;
        |                                - Keyword only&lt;br /&gt;
         -- Positional only&amp;lt;/syntaxhighlight&amp;gt;&amp;#039;&amp;#039;&amp;#039;This is the typical case:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/tutorial/controlflow.html#positional-or-keyword-arguments Positional-or-Keyword Arguments] &amp;amp;#x3E; &amp;#039;&amp;#039;&amp;#039;If / and * are not present&amp;#039;&amp;#039;&amp;#039; in the function definition, arguments may be passed to a function by position or by keyword.&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments Keyword Arguments]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;This is an optional topic!&amp;#039;&amp;#039;&amp;#039;&amp;lt;blockquote&amp;gt;Functions can also be called using keyword arguments of the form kwarg=value.&amp;lt;/blockquote&amp;gt;[https://docs.python.org/3/tutorial/controlflow.html#positional-only-parameters Positional-Only Parameters]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;This is an optional topic!&amp;#039;&amp;#039;&amp;#039;&amp;lt;blockquote&amp;gt;If positional-only, the parameters’ order matters, and the parameters cannot be passed by keyword.&lt;br /&gt;
&lt;br /&gt;
If there is no / in the function definition, there are no positional-only parameters.&amp;lt;/blockquote&amp;gt;[https://docs.python.org/3/tutorial/controlflow.html#keyword-only-arguments Keyword-Only Arguments]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;This is an optional topic!&amp;#039;&amp;#039;&amp;#039;&amp;lt;blockquote&amp;gt;To mark parameters as keyword-only, indicating the parameters must be passed by keyword argument, place an * in the arguments list just before the first keyword-only parameter.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists Arbitrary Argument Lists] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def f(p1, *arguments, **keywords):&lt;br /&gt;
    print(&amp;quot;p1:&amp;quot;)&lt;br /&gt;
    print(p1)&lt;br /&gt;
    print()&lt;br /&gt;
&lt;br /&gt;
    print(&amp;quot;*arguments&amp;quot;)&lt;br /&gt;
    for arg in arguments:&lt;br /&gt;
        print(arg)&lt;br /&gt;
    print()&lt;br /&gt;
&lt;br /&gt;
    print(&amp;quot;**keywords&amp;quot;)&lt;br /&gt;
    for kw in keywords:&lt;br /&gt;
        print(f&amp;quot;{kw}: {keywords[kw]}&amp;quot;)&lt;br /&gt;
    print()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
f(1, 2, 3, a=4, b=5)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;p1:&lt;br /&gt;
1&lt;br /&gt;
&lt;br /&gt;
*arguments&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
&lt;br /&gt;
**keywords&lt;br /&gt;
a: 4&lt;br /&gt;
b: 5&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists Unpacking Argument Lists] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def f(a: int, b: int, c: int, d: int) -&amp;gt; int:&lt;br /&gt;
    return a * b * c * d&lt;br /&gt;
&lt;br /&gt;
print(f(2, 3, 4, 5))  # -&amp;gt; 120&lt;br /&gt;
&lt;br /&gt;
my_list_a = [2, 3, 4]&lt;br /&gt;
my_list_b = [2, 3, 4, 5]&lt;br /&gt;
my_list_c = [3, 4, 5]&lt;br /&gt;
print(f(*my_list_a, 5))  # -&amp;gt; 120 (plus a false warning: &amp;#039;Too many arguments for &amp;quot;f&amp;quot;&amp;#039; from MyPy)&lt;br /&gt;
print(f(*my_list_b))  # -&amp;gt; 120&lt;br /&gt;
print(f(2, *my_list_c))  # -&amp;gt; 120&lt;br /&gt;
&lt;br /&gt;
my_tuple_c = [3, 4, 5]&lt;br /&gt;
print(f(2, *my_tuple_c))  # -&amp;gt; 120&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://peps.python.org/pep-0257/ Documentation strings​] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;This is a universal function.&lt;br /&gt;
    It does nothing.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
help(my_function)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;Help on function my_function in module __main__:&lt;br /&gt;
&lt;br /&gt;
my_function()&lt;br /&gt;
    This is a universal function.&lt;br /&gt;
    It does nothing.&amp;lt;/syntaxhighlight&amp;gt;One liner:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;This is a universal function.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    pass&amp;lt;/syntaxhighlight&amp;gt;Novelization:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;This is a universal function.&lt;br /&gt;
        a&lt;br /&gt;
        b&lt;br /&gt;
        d&lt;br /&gt;
        end&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    pass&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/reference/expressions.html#lambda Lambda expressions / anonymous functions​] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;lambda_expr ::=  &amp;quot;lambda&amp;quot; [parameter_list] &amp;quot;:&amp;quot; expression&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda parameters: expression yields a function object. The unnamed object behaves like a function object defined with:&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def &amp;lt;lambda&amp;gt;(parameters):&lt;br /&gt;
    return expression&amp;lt;/syntaxhighlight&amp;gt;Example 1:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;pairs = [(1, &amp;quot;one&amp;quot;), (2, &amp;quot;two&amp;quot;), (3, &amp;quot;three&amp;quot;), (4, &amp;quot;four&amp;quot;)]&lt;br /&gt;
&lt;br /&gt;
def f(x):&lt;br /&gt;
    return x[1]&lt;br /&gt;
&lt;br /&gt;
print(pairs) # -&amp;gt; [(1, &amp;#039;one&amp;#039;), (2, &amp;#039;two&amp;#039;), (3, &amp;#039;three&amp;#039;), (4, &amp;#039;four&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
print((lambda x: x[1])(pairs)) # -&amp;gt; (2, &amp;#039;two&amp;#039;)&lt;br /&gt;
print(f(pairs)) # -&amp;gt; (2, &amp;#039;two&amp;#039;)&amp;lt;/syntaxhighlight&amp;gt;Example 2:&lt;br /&gt;
&lt;br /&gt;
With a normal function:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;pairs = [(1, &amp;quot;one&amp;quot;), (2, &amp;quot;two&amp;quot;), (3, &amp;quot;three&amp;quot;), (4, &amp;quot;four&amp;quot;)]&lt;br /&gt;
&lt;br /&gt;
def f(x):&lt;br /&gt;
    return x[1]&lt;br /&gt;
&lt;br /&gt;
print(pairs)  # -&amp;gt; [(1, &amp;#039;one&amp;#039;), (2, &amp;#039;two&amp;#039;), (3, &amp;#039;three&amp;#039;), (4, &amp;#039;four&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
pairs.sort(key=f) &lt;br /&gt;
print(pairs) # -&amp;gt; [(4, &amp;#039;four&amp;#039;), (1, &amp;#039;one&amp;#039;), (3, &amp;#039;three&amp;#039;), (2, &amp;#039;two&amp;#039;)]&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;pairs = [(1, &amp;quot;one&amp;quot;), (2, &amp;quot;two&amp;quot;), (3, &amp;quot;three&amp;quot;), (4, &amp;quot;four&amp;quot;)]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(pairs)  # -&amp;gt; [(1, &amp;#039;one&amp;#039;), (2, &amp;#039;two&amp;#039;), (3, &amp;#039;three&amp;#039;), (4, &amp;#039;four&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
pairs.sort(key=(lambda x: x[1]))&lt;br /&gt;
print(pairs) # -&amp;gt; [(4, &amp;#039;four&amp;#039;), (1, &amp;#039;one&amp;#039;), (3, &amp;#039;three&amp;#039;), (2, &amp;#039;two&amp;#039;)]&amp;lt;/syntaxhighlight&amp;gt;Small warning:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;print(pairs.sort()) # -&amp;gt; None&amp;lt;/syntaxhighlight&amp;gt;because &amp;#039;&amp;#039;&amp;#039;.sort()&amp;#039;&amp;#039;&amp;#039; is an inplace function.&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>