diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb
index c48d0de..3ea736a 100644
--- a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb
+++ b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
- "signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
+ "signature": "sha256:9a07a78204a51f0faab65e52657f0446cd604ed470627f9c6af1ba74c047fe23"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -80,7 +80,8 @@
"- [Key differences between Python 2 and 3](#python_differences)\n",
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
"- [Abortive statements in `finally` blocks](#finally_blocks)\n",
- "- [Assigning types to variables as values](#variable_types)"
+ "- [Assigning types to variables as values](#variable_types)\n",
+ "- [Keyword argument unpacking syntax - `*args` and `**kwargs`](#splat_op)"
]
},
{
@@ -2979,6 +2980,125 @@
],
"prompt_number": 4
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
\n",
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Keyword argument unpacking syntax - `*args` and `**kwargs`"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Python has a very convenient \"keyword argument unpacking syntax\" (often also referred to as \"splat\"-operators). This is particularly useful, if we want to define a function that can take a arbitrary number of input arguments."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### Single-asterisk (*args)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def a_func(*args):\n",
+ " print('type of args:', type(args))\n",
+ " print('args contents:', args)\n",
+ " print('1st argument:', args[0])\n",
+ "\n",
+ "a_func(0, 1, 'a', 'b', 'c')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "type of args: \n",
+ "args contents: (0, 1, 'a', 'b', 'c')\n",
+ "1st argument: 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### Double-asterisk (**kwargs)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def b_func(**kwargs):\n",
+ " print('type of kwargs:', type(kwargs))\n",
+ " print('kwargs contents: ', kwargs)\n",
+ " print('value of argument a:', kwargs['a'])\n",
+ " \n",
+ "b_func(a=1, b=2, c=3, d=4)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "type of kwargs: \n",
+ "kwargs contents: {'d': 4, 'c': 3, 'b': 2, 'a': 1}\n",
+ "value of argument a: 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 35
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### (Partially) unpacking of iterables\n",
+ "Another useful application of the \"unpacking\"-operator is the unpacking of lists and other other iterables."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "val1, *vals = [1, 2, 3, 4, 5]\n",
+ "print('val1:', val1)\n",
+ "print('vals:', vals)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "val1: 1\n",
+ "vals: [2, 3, 4, 5]\n"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -3008,6 +3128,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "#### 05/01/2014\n",
+ "- new section: keyword argument unpacking syntax\n",
+ "\n",
"#### 04/27/2014\n",
"- minor fixes of typos\n",
"- single- vs. double-underscore clarification in the private class section."
diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb
index c48d0de..3ea736a 100644
--- a/not_so_obvious_python_stuff.ipynb
+++ b/not_so_obvious_python_stuff.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
- "signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
+ "signature": "sha256:9a07a78204a51f0faab65e52657f0446cd604ed470627f9c6af1ba74c047fe23"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -80,7 +80,8 @@
"- [Key differences between Python 2 and 3](#python_differences)\n",
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
"- [Abortive statements in `finally` blocks](#finally_blocks)\n",
- "- [Assigning types to variables as values](#variable_types)"
+ "- [Assigning types to variables as values](#variable_types)\n",
+ "- [Keyword argument unpacking syntax - `*args` and `**kwargs`](#splat_op)"
]
},
{
@@ -2979,6 +2980,125 @@
],
"prompt_number": 4
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\n",
+ "
\n",
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Keyword argument unpacking syntax - `*args` and `**kwargs`"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Python has a very convenient \"keyword argument unpacking syntax\" (often also referred to as \"splat\"-operators). This is particularly useful, if we want to define a function that can take a arbitrary number of input arguments."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### Single-asterisk (*args)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def a_func(*args):\n",
+ " print('type of args:', type(args))\n",
+ " print('args contents:', args)\n",
+ " print('1st argument:', args[0])\n",
+ "\n",
+ "a_func(0, 1, 'a', 'b', 'c')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "type of args: \n",
+ "args contents: (0, 1, 'a', 'b', 'c')\n",
+ "1st argument: 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### Double-asterisk (**kwargs)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def b_func(**kwargs):\n",
+ " print('type of kwargs:', type(kwargs))\n",
+ " print('kwargs contents: ', kwargs)\n",
+ " print('value of argument a:', kwargs['a'])\n",
+ " \n",
+ "b_func(a=1, b=2, c=3, d=4)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "type of kwargs: \n",
+ "kwargs contents: {'d': 4, 'c': 3, 'b': 2, 'a': 1}\n",
+ "value of argument a: 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 35
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### (Partially) unpacking of iterables\n",
+ "Another useful application of the \"unpacking\"-operator is the unpacking of lists and other other iterables."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "val1, *vals = [1, 2, 3, 4, 5]\n",
+ "print('val1:', val1)\n",
+ "print('vals:', vals)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "val1: 1\n",
+ "vals: [2, 3, 4, 5]\n"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -3008,6 +3128,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "#### 05/01/2014\n",
+ "- new section: keyword argument unpacking syntax\n",
+ "\n",
"#### 04/27/2014\n",
"- minor fixes of typos\n",
"- single- vs. double-underscore clarification in the private class section."