Skip to content

Instantly share code, notes, and snippets.

@chetanambi
Last active February 20, 2021 07:47
Show Gist options
  • Save chetanambi/1f12e8d17475831a647e4b9f847f96fc to your computer and use it in GitHub Desktop.
Save chetanambi/1f12e8d17475831a647e4b9f847f96fc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__In the below function definition, `a` is a positional parameter and `args` holds an arbitrary number of left-over positional parameters.__"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def func(a, *args):\n",
" print(f\"a:{a}, args:{args}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__In the function call `func(1,2,3,4,5)`, argument `1` is mapped to `a` and the remaining arguments will be part of `args` and args return its content as a tuple `(2,3,4,5)`.__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:1, args:(2, 3, 4, 5)\n"
]
}
],
"source": [
"func(1,2,3,4,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__`func(a=1,2,3,4,5)` raises `TypeError` because position arguments must not follow keyword arguments. Here positional arguments `2,3,4,5` follow keyword argument which raise `TypeError`.__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "positional argument follows keyword argument (<ipython-input-3-5aeade47c095>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-3-5aeade47c095>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m func(a=1,2,3,4,5)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
]
}
],
"source": [
"func(a=1,2,3,4,5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment