Skip to content

Instantly share code, notes, and snippets.

@chetanambi
Last active February 20, 2021 07:27
Show Gist options
  • Save chetanambi/ce278ce451bfbaf1fd025aca393d7e5a to your computer and use it in GitHub Desktop.
Save chetanambi/ce278ce451bfbaf1fd025aca393d7e5a 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 & b are positional parameters and c is the keyword parameter with a default value of 30.__"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def func(a, b, c=30):\n",
" print(f\"a:{a}, b:{b}, c:{c}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__In the function call `func(10, 20)`, arguments `10` and `20` are mapped to `a` & `b` by position. Since `c` isn't passed as an argument, it gets the default value i.e. `30`.__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:10, b:20, c:30\n"
]
}
],
"source": [
"func(10, 20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Similarly, `func(20, 10)` works the same as above but the values of `a` & `b` are interchanged as expected.__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:20, b:10, c:30\n"
]
}
],
"source": [
"func(20, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__`a` & `b` can also be passed as keyword arguments. Hence `func(a=10, b=20, c=30)` works without any errors.__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:10, b:20, c:30\n"
]
}
],
"source": [
"func(a=10, b=20, c=30)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:10, b:20, c:30\n"
]
}
],
"source": [
"func(b=20, a=10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__The call `func(a=20)` gives an error because the function expects 2 positional arguments (`a` & `b`), but we are only passing one argument `a`.__"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "func() missing 1 required positional argument: 'b'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-6-58396073df84>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: func() missing 1 required positional argument: 'b'"
]
}
],
"source": [
"func(a=10)"
]
}
],
"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