{ "cells": [ { "cell_type": "markdown", "id": "f3c6c7f0", "metadata": {}, "source": [ "# Positional and Keyword Arguments" ] }, { "cell_type": "markdown", "id": "3f0cbd3a", "metadata": {}, "source": [ "## Posisional Arguments\n", "\n", "Most common way of assigning arguments to parameters: via the **order** in which they are passed.\n", "\n", "i.e. their **position**" ] }, { "cell_type": "code", "execution_count": 39, "id": "09e8fbbd", "metadata": {}, "outputs": [], "source": [ "def my_func(a, b):\n", " print(f\"{a=}, {b=}\")" ] }, { "cell_type": "code", "execution_count": 40, "id": "350e86db", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=10, b=20\n" ] } ], "source": [ "my_func(10, 20)" ] }, { "cell_type": "code", "execution_count": 41, "id": "d7ab7b4a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=20, b=10\n" ] } ], "source": [ "my_func(20, 10)" ] }, { "cell_type": "code", "execution_count": 42, "id": "8d04e41a", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "my_func() missing 1 required positional argument: 'b'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[42], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: my_func() missing 1 required positional argument: 'b'" ] } ], "source": [ "my_func(1)" ] }, { "cell_type": "code", "execution_count": 43, "id": "c7676e7d", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "my_func() takes 2 positional arguments but 3 were given", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[43], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: my_func() takes 2 positional arguments but 3 were given" ] } ], "source": [ "my_func(1, 2, 3)" ] }, { "cell_type": "markdown", "id": "e58bf53c", "metadata": {}, "source": [ "## Default Values\n", "\n", "A positional arguments can be made **optional** by specifying a **default value** for the corresponding parameter." ] }, { "cell_type": "code", "execution_count": 20, "id": "74884f73", "metadata": {}, "outputs": [], "source": [ "def my_func(a, b=100):\n", " print(f\"{a=}, {b=}\")" ] }, { "cell_type": "code", "execution_count": 21, "id": "8cc20a28", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=10, b=20\n" ] } ], "source": [ "my_func(10, 20)" ] }, { "cell_type": "code", "execution_count": 22, "id": "ba60ec0b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=5, b=100\n" ] } ], "source": [ "my_func(5)" ] }, { "cell_type": "markdown", "id": "51220d35", "metadata": {}, "source": [ "Consider a case where we have three arguments, and we want to make one of theme optional:" ] }, { "cell_type": "markdown", "id": "fdd35227", "metadata": {}, "source": [ "```python\n", "def my_func(a, b=100, c):\n", " pass\n", "```\n", "\n", "How would we call this function without specifying a value for the second parameters?\n", "\n", "```python\n", "my_func(5, 25) ???\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "id": "5a972c98", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "non-default argument follows default argument (983498564.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn[6], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m def my_func(a, b=100, c):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" ] } ], "source": [ "def my_func(a, b=100, c):\n", " pass" ] }, { "cell_type": "markdown", "id": "d09662c8", "metadata": {}, "source": [ "If a positional parameter is defined with a didault value\n", " \n", "**every** positional parameter after it\n", "\n", "**must** also be given a difault value." ] }, { "cell_type": "code", "execution_count": 23, "id": "c525980e", "metadata": {}, "outputs": [], "source": [ "def my_func(a, b=5, c=10):\n", " print(f\"{a=}, {b=}, {c=}\")" ] }, { "cell_type": "code", "execution_count": 24, "id": "f2bfbc75", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=5, c=10\n" ] } ], "source": [ "my_func(1)" ] }, { "cell_type": "code", "execution_count": 25, "id": "1b9d29c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=2, c=10\n" ] } ], "source": [ "my_func(1, 2)" ] }, { "cell_type": "code", "execution_count": 26, "id": "a59a2517", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=2, c=3\n" ] } ], "source": [ "my_func(1, 2, 3)" ] }, { "cell_type": "markdown", "id": "0aa7a2c3", "metadata": {}, "source": [ "But what if we want to specify $1^{st}$ and $3^{rd}$ arguments, but omit the $2^{nd}$ argument?\n", "\n", "i.e. we want to specify values for `a` and `c`, but let `b` take on its difault value?" ] }, { "cell_type": "markdown", "id": "2ae0f362", "metadata": {}, "source": [ "## Keyword Arguments (named argumnets)" ] }, { "cell_type": "code", "execution_count": 27, "id": "a80ba0f7", "metadata": {}, "outputs": [], "source": [ "def my_func(a, b=5, c=10):\n", " print(f\"{a=}, {b=}, {c=}\")" ] }, { "cell_type": "code", "execution_count": 28, "id": "55dc2a6a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=5, c=2\n" ] } ], "source": [ "my_func(a=1, c=2)" ] }, { "cell_type": "code", "execution_count": 29, "id": "81bb6427", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=5, c=2\n" ] } ], "source": [ "my_func(1, c=2)" ] }, { "cell_type": "markdown", "id": "759ca484", "metadata": {}, "source": [ "Positional arguments can, **optionally**, be specified by using the parameter name.\n", "\n", "Whether or not the parameters have default values" ] }, { "cell_type": "code", "execution_count": 1, "id": "a96816cf", "metadata": {}, "outputs": [], "source": [ "def my_func(a, b, c):\n", " # code hare\n", " pass" ] }, { "cell_type": "markdown", "id": "a2666893", "metadata": {}, "source": [ "```python\n", "my_func(1, 2, 3)\n", "my_func(1, 2, c=3)\n", "my_func(a=1, b=2, c=3)\n", "my_func(c=3, a=1, b=2)\n", "```\n", "\n", "`a = 1`, `b = 2`, `c = 3`" ] }, { "cell_type": "markdown", "id": "4df3ee0e", "metadata": {}, "source": [ "But once you use a named argument, all parameters **thereafter must** be named too." ] }, { "cell_type": "code", "execution_count": 5, "id": "dae4ac29", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "positional argument follows keyword argument (3895868150.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn[5], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m my_func(c=1, 2, 3)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" ] } ], "source": [ "my_func(c=1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 6, "id": "ca08245f", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "positional argument follows keyword argument (2003381380.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn[6], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m my_func(1, b=2, 3)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" ] } ], "source": [ "my_func(1, b=2, 3)" ] }, { "cell_type": "code", "execution_count": 30, "id": "f1e2d0ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=2, c=3\n" ] } ], "source": [ "my_func(1, 2, c=3)" ] }, { "cell_type": "markdown", "id": "81b049f3", "metadata": {}, "source": [ "All arguments after the first named (keyword) arguments, must be named too.\n", "\n", "Default arguments may still ne omitted." ] }, { "cell_type": "code", "execution_count": 33, "id": "d1ef5052", "metadata": {}, "outputs": [], "source": [ "def my_func(a, b=2, c=3):\n", " print(f\"{a=}, {b=}, {c=}\")" ] }, { "cell_type": "code", "execution_count": 34, "id": "fd0236b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=2, c=3\n" ] } ], "source": [ "my_func(1)" ] }, { "cell_type": "code", "execution_count": 35, "id": "2bc17243", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=5, c=3\n" ] } ], "source": [ "my_func(a=1, b=5)" ] }, { "cell_type": "code", "execution_count": 36, "id": "0a95be80", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=1, b=2, c=0\n" ] } ], "source": [ "my_func(c=0, a=1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }