mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-31 05:33:47 +00:00
keyword unpacking syntax
This commit is contained in:
parent
a0edd6b293
commit
6253c112f2
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
|
"signature": "sha256:9a07a78204a51f0faab65e52657f0446cd604ed470627f9c6af1ba74c047fe23"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -80,7 +80,8 @@
|
||||||
"- [Key differences between Python 2 and 3](#python_differences)\n",
|
"- [Key differences between Python 2 and 3](#python_differences)\n",
|
||||||
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
|
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
|
||||||
"- [Abortive statements in `finally` blocks](#finally_blocks)\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
|
"prompt_number": 4
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<a name=\"splat_op\"></a>\n",
|
||||||
|
"<br>\n",
|
||||||
|
"<br>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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: <class 'tuple'>\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: <class 'dict'>\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",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -3008,6 +3128,9 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
|
"#### 05/01/2014\n",
|
||||||
|
"- new section: keyword argument unpacking syntax\n",
|
||||||
|
"\n",
|
||||||
"#### 04/27/2014\n",
|
"#### 04/27/2014\n",
|
||||||
"- minor fixes of typos\n",
|
"- minor fixes of typos\n",
|
||||||
"- single- vs. double-underscore clarification in the private class section."
|
"- single- vs. double-underscore clarification in the private class section."
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
|
"signature": "sha256:9a07a78204a51f0faab65e52657f0446cd604ed470627f9c6af1ba74c047fe23"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -80,7 +80,8 @@
|
||||||
"- [Key differences between Python 2 and 3](#python_differences)\n",
|
"- [Key differences between Python 2 and 3](#python_differences)\n",
|
||||||
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
|
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
|
||||||
"- [Abortive statements in `finally` blocks](#finally_blocks)\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
|
"prompt_number": 4
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<a name=\"splat_op\"></a>\n",
|
||||||
|
"<br>\n",
|
||||||
|
"<br>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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: <class 'tuple'>\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: <class 'dict'>\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",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -3008,6 +3128,9 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
|
"#### 05/01/2014\n",
|
||||||
|
"- new section: keyword argument unpacking syntax\n",
|
||||||
|
"\n",
|
||||||
"#### 04/27/2014\n",
|
"#### 04/27/2014\n",
|
||||||
"- minor fixes of typos\n",
|
"- minor fixes of typos\n",
|
||||||
"- single- vs. double-underscore clarification in the private class section."
|
"- single- vs. double-underscore clarification in the private class section."
|
||||||
|
|
Loading…
Reference in New Issue
Block a user