Added *args, **kwargs snippet.

This commit is contained in:
Donne Martin 2015-01-26 16:59:02 -05:00
parent 84fb0356bf
commit 90a0fd501f

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:1c8db5161bc70c65141de06823a3be29e8f58e1668a4090282d39002eabd8adb"
"signature": "sha256:a3a2ee34a40ca6d18902bc7ba52393ce71aa5dda1937d48cdd0db3044dc235bf"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -150,7 +150,7 @@
"core.tests.test_transform_util.TestTransformUtil.test_remove_punctuation ... ok\r\n",
"\r\n",
"----------------------------------------------------------------------\r\n",
"Ran 3 tests in 0.002s\r\n",
"Ran 3 tests in 0.001s\r\n",
"\r\n",
"OK\r\n"
]
@ -193,13 +193,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"prompt_number": 4,
"text": [
"['f', 'b', 'fo', 'ba', 'foo', 'baz', 'bar,']"
]
}
],
"prompt_number": 7
"prompt_number": 4
},
{
"cell_type": "markdown",
@ -238,7 +238,7 @@
]
}
],
"prompt_number": 31
"prompt_number": 5
},
{
"cell_type": "markdown",
@ -266,7 +266,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 34
"prompt_number": 6
},
{
"cell_type": "code",
@ -282,13 +282,56 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 46,
"prompt_number": 7,
"text": [
"[False, True, False, False, False, False, False, True, True, True]"
]
}
],
"prompt_number": 46
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## \\*args, \\*\\*kwargs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\*args and \\*\\*kwargs are useful when you don't know how many arguments might be passed to your function or to handle named arguments that you have not defined in advance."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def foo(func, arg, *args, **kwargs):\n",
" print('arg: %s', arg)\n",
" print('args: %s', args)\n",
" print('kwargs: %s', kwargs)\n",
" \n",
" print('func result: %s', func(args))\n",
"\n",
"foo(sum, \"foo\", 1, 2, 3, 4, 5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('arg: %s', 'foo')\n",
"('args: %s', (1, 2, 3, 4, 5))\n",
"('kwargs: %s', {})\n",
"('func result: %s', 15)\n"
]
}
],
"prompt_number": 8
}
],
"metadata": {}