Created
July 3, 2014 17:18
-
-
Save rdhyee/f7ebb03f2bf7ce54f20a to your computer and use it in GitHub Desktop.
I needed to remind myself how to handle possibly unexpected keyword arguments in Python 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:8ea06d90e452fed356c1560264f61c6c7a23ff751cc3771864ac23ed91a38b6d" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Question: \n", | |
"\n", | |
"if I have a function \n", | |
"\n", | |
"````Python\n", | |
"def f(a,b,c='default for c'):\n", | |
" print a, b, c\n", | |
" \n", | |
"````\n", | |
"\n", | |
"and I want to pass it variables other than a,b,c, can I do so with **kwargs?\n", | |
"\n", | |
"The way variables are handled might be different in Python 3.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(a,b,c='default for c'):\n", | |
" print a, b, c" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f(1,2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 2 default for c\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def g(a,b,c='default for c', **kwargs):\n", | |
" print a, b, c, kwargs" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"g(1,2, dog='cat')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 2 default for c {'dog': 'cat'}\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Tentative conclusion: **kwargs will suck up kw args notexplicity named." | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment