|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b9a2b9ad-2e5b-46b0-bad7-bc0d78d05d7e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Concept of *args and **kwargs" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "b671e944-f2bb-4141-a09f-410f83793299", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "In Python, it's possible to define functions that accept a variable number of arguments. This can be useful when we don't know in advance how many arguments a function will need to handle, or when we want to make our code more flexible. Python uses these special symbols for passing arguments:\n", |
| 17 | + "\n", |
| 18 | + "* *args (For Non-Keyword Arguments)\n", |
| 19 | + "* **kwargs (For Keyword Arguments)" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "id": "ad5d0706-85ce-4eb9-90ee-2dd03f41302e", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "### *args (For Non-Keyword Arguments)" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "id": "b4f2f2b3-c66f-41a9-8f8d-cce5d4669159", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "In Python, *args is a special syntax used to pass a variable number of positional arguments to a function. The *args notation allows us to pass any number of arguments to a function, and those arguments will be collected into a tuple.\n", |
| 36 | + "\n", |
| 37 | + "For example, if we want to make an addition function that supports taking any number of arguments and able to add them all together. In this case we can use *args.\n", |
| 38 | + "\n", |
| 39 | + "One important thing to note is that *args must come at the end of a function's parameter list, after any named arguments. This is because *args collects all positional arguments that are not matched to named parameters, so any named parameters that come after *args will be ignored" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 1, |
| 45 | + "id": "e7ed9a8d-7c98-4cc5-826d-27a3e79050a3", |
| 46 | + "metadata": { |
| 47 | + "tags": [] |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "def sum(*args):\n", |
| 52 | + " s = 0\n", |
| 53 | + " for i in args:\n", |
| 54 | + " s += i\n", |
| 55 | + " print(s)" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 2, |
| 61 | + "id": "b84d279d-5c51-4885-a0d3-c6ad2b209e6c", |
| 62 | + "metadata": { |
| 63 | + "tags": [] |
| 64 | + }, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stdout", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "15\n" |
| 71 | + ] |
| 72 | + } |
| 73 | + ], |
| 74 | + "source": [ |
| 75 | + "sum(1, 2, 3, 4, 5)" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "id": "9d851610-86d3-40ab-8be6-e486699829e1", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "### **kwargs (For Keyword Arguments)" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "markdown", |
| 88 | + "id": "1c8a2150-01f5-48ef-a844-e5975797b361", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "In Python, **kwargs is a special syntax used to pass a variable number of keyword arguments to a function. The **kwargs notation allows us to pass any number of keyword arguments to a function, and those arguments will be collected into a dictionary.\n", |
| 92 | + "\n", |
| 93 | + "A keyword argument is where we provide a name to the variable as we pass it into the function.\n", |
| 94 | + "\n", |
| 95 | + "One important thing to note is that **kwargs must come after *args in a function's parameter list, if both are used. This is because *args collects all positional arguments that are not matched to named parameters, while **kwargs collects all keyword arguments that are not matched to named parameters." |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 3, |
| 101 | + "id": "cbe35c5b-8d5b-48ca-9fb2-67d3a7c2ac35", |
| 102 | + "metadata": { |
| 103 | + "tags": [] |
| 104 | + }, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "def myFunction(**kwargs):\n", |
| 108 | + " for key, value in kwargs.items():\n", |
| 109 | + " print(key, \":\", value)" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 4, |
| 115 | + "id": "493b7377-650e-4eda-9ffd-ef75b96245db", |
| 116 | + "metadata": { |
| 117 | + "tags": [] |
| 118 | + }, |
| 119 | + "outputs": [ |
| 120 | + { |
| 121 | + "name": "stdout", |
| 122 | + "output_type": "stream", |
| 123 | + "text": [ |
| 124 | + "name : Krishnagopal Halder\n", |
| 125 | + "age : 22\n" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "myFunction(name=\"Krishnagopal Halder\", age=22)" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "id": "8158ea12-fc2b-4687-b4f0-57eb86941806", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "### Example with *args and **kwargs" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 5, |
| 144 | + "id": "2303d1f2-7a93-4687-809e-db26d80d94f5", |
| 145 | + "metadata": { |
| 146 | + "tags": [] |
| 147 | + }, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "def personalInfo(name, age, *args, **kwargs):\n", |
| 151 | + " print(\"The name of the peron is\", name)\n", |
| 152 | + " print(\"The age of the person is\", age)\n", |
| 153 | + " print(\"Other informations:\")\n", |
| 154 | + " for key, value in kwargs.items():\n", |
| 155 | + " print(key, \":\", value)\n", |
| 156 | + " print(\"Unknown informations:\")\n", |
| 157 | + " for i in args:\n", |
| 158 | + " print(i) " |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 6, |
| 164 | + "id": "792ef8be-ac49-415f-a455-eda94a2f2802", |
| 165 | + "metadata": { |
| 166 | + "tags": [] |
| 167 | + }, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "name": "stdout", |
| 171 | + "output_type": "stream", |
| 172 | + "text": [ |
| 173 | + "The name of the peron is Krishnagopal Halder\n", |
| 174 | + "The age of the person is 22\n", |
| 175 | + "Other informations:\n", |
| 176 | + "state : WB\n", |
| 177 | + "country : India\n", |
| 178 | + "Unknown informations:\n", |
| 179 | + "First arg\n", |
| 180 | + "Second arg\n" |
| 181 | + ] |
| 182 | + } |
| 183 | + ], |
| 184 | + "source": [ |
| 185 | + "personalInfo(\"Krishnagopal Halder\", 22, \"First arg\", \"Second arg\", state=\"WB\", country=\"India\")" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "metadata": { |
| 190 | + "kernelspec": { |
| 191 | + "display_name": "Python 3 (ipykernel)", |
| 192 | + "language": "python", |
| 193 | + "name": "python3" |
| 194 | + }, |
| 195 | + "language_info": { |
| 196 | + "codemirror_mode": { |
| 197 | + "name": "ipython", |
| 198 | + "version": 3 |
| 199 | + }, |
| 200 | + "file_extension": ".py", |
| 201 | + "mimetype": "text/x-python", |
| 202 | + "name": "python", |
| 203 | + "nbconvert_exporter": "python", |
| 204 | + "pygments_lexer": "ipython3", |
| 205 | + "version": "3.9.16" |
| 206 | + } |
| 207 | + }, |
| 208 | + "nbformat": 4, |
| 209 | + "nbformat_minor": 5 |
| 210 | +} |
0 commit comments