|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/07_Python_Advanced_Topics)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python __`*args`__ and __`**kwargs`__\n", |
| 17 | + "\n", |
| 18 | + "In this class, we will learn about Python __`*args`__ and __`**kwargs`__ ,their uses and functions with examples." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "In programming, we define a function to make a reusable code that performs similar operation. To perform that operation, we call a function with the specific value, this value is called a function argument in Python.\n", |
| 26 | + "\n", |
| 27 | + "We would recommend you to read **[Python Function](https://github.com/milaan9/04_Python_Functions/blob/main/001_Python_Functions.ipynb)** and **[Python Function Arguments](https://github.com/milaan9/04_Python_Functions/blob/main/004_Python_Function_Arguments.ipynb)**.\n", |
| 28 | + "\n", |
| 29 | + "Suppose, we define a function for addition of 3 numbers." |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 1, |
| 35 | + "metadata": { |
| 36 | + "ExecuteTime": { |
| 37 | + "end_time": "2021-07-31T13:08:44.472274Z", |
| 38 | + "start_time": "2021-07-31T13:08:44.459576Z" |
| 39 | + }, |
| 40 | + "scrolled": false |
| 41 | + }, |
| 42 | + "outputs": [ |
| 43 | + { |
| 44 | + "name": "stdout", |
| 45 | + "output_type": "stream", |
| 46 | + "text": [ |
| 47 | + "sum: 46\n" |
| 48 | + ] |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "# Example 1: Function to add 3 numbers\n", |
| 53 | + "\n", |
| 54 | + "def adder(x,y,z):\n", |
| 55 | + " print(\"sum:\",x+y+z)\n", |
| 56 | + "\n", |
| 57 | + "adder(12,15,19)" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "**Explanation:**\n", |
| 65 | + "\n", |
| 66 | + "In above program we have **`adder()`** function with three arguments **`x`**, **`y`** and **`z`**. When we pass three values while calling **`adder()`** function, we get sum of the 3 numbers as the output." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "markdown", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "Lets see what happens when we pass more than 3 arguments in the **`adder()`** function." |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 2, |
| 79 | + "metadata": { |
| 80 | + "ExecuteTime": { |
| 81 | + "end_time": "2021-07-31T13:08:44.903013Z", |
| 82 | + "start_time": "2021-07-31T13:08:44.475203Z" |
| 83 | + }, |
| 84 | + "scrolled": true |
| 85 | + }, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "ename": "TypeError", |
| 89 | + "evalue": "adder() takes 3 positional arguments but 5 were given", |
| 90 | + "output_type": "error", |
| 91 | + "traceback": [ |
| 92 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 93 | + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 94 | + "\u001b[1;32m<ipython-input-2-986e6d871071>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"sum:\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0madder\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m15\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m25\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 95 | + "\u001b[1;31mTypeError\u001b[0m: adder() takes 3 positional arguments but 5 were given" |
| 96 | + ] |
| 97 | + } |
| 98 | + ], |
| 99 | + "source": [ |
| 100 | + "def adder(x,y,z):\n", |
| 101 | + " print(\"sum:\",x+y+z)\n", |
| 102 | + "\n", |
| 103 | + "adder(5,10,15,20,25)" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "**Explanation:**\n", |
| 111 | + "\n", |
| 112 | + "In the above program, we passed 5 arguments to the **`adder()`** function instead of 3 arguments due to which we got **`TypeError`**." |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "## Introduction to __`*args`__ and __`**kwargs`__ in Python\n", |
| 120 | + "\n", |
| 121 | + "In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:\n", |
| 122 | + "\n", |
| 123 | + "* __`*args`__ (Non Keyword Arguments)\n", |
| 124 | + "* __`**kwargs`__ (Keyword Arguments)\n", |
| 125 | + "\n", |
| 126 | + "We use __`*args`__ and __`**kwargs`__ as an argument when we are unsure about the number of arguments to pass in the functions." |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "metadata": {}, |
| 132 | + "source": [ |
| 133 | + "## Python __*args__\n", |
| 134 | + "\n", |
| 135 | + "As in the above example we are not sure about the number of arguments that can be passed to a function. Python has __`*args`__ which allow us to pass the variable number of non keyword arguments to function.\n", |
| 136 | + "\n", |
| 137 | + "In the function, we should use an asterisk **`*`** before the parameter name to pass variable length arguments.The arguments are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk **`*`**." |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": 3, |
| 143 | + "metadata": { |
| 144 | + "ExecuteTime": { |
| 145 | + "end_time": "2021-07-31T13:08:51.348994Z", |
| 146 | + "start_time": "2021-07-31T13:08:51.327507Z" |
| 147 | + } |
| 148 | + }, |
| 149 | + "outputs": [ |
| 150 | + { |
| 151 | + "name": "stdout", |
| 152 | + "output_type": "stream", |
| 153 | + "text": [ |
| 154 | + "Sum: 9\n", |
| 155 | + "Sum: 21\n", |
| 156 | + "Sum: 21\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "# Example 2: Using *args to pass the variable length arguments to the function\n", |
| 162 | + "\n", |
| 163 | + "def adder(*num):\n", |
| 164 | + " sum = 0\n", |
| 165 | + " \n", |
| 166 | + " for n in num:\n", |
| 167 | + " sum = sum + n\n", |
| 168 | + "\n", |
| 169 | + " print(\"Sum:\",sum)\n", |
| 170 | + "\n", |
| 171 | + "adder(3,6)\n", |
| 172 | + "adder(3,5,6,7)\n", |
| 173 | + "adder(1,2,3,6,9)" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "markdown", |
| 178 | + "metadata": {}, |
| 179 | + "source": [ |
| 180 | + "**Explanation:**\n", |
| 181 | + "\n", |
| 182 | + "In the above program, we used __`*num`__ as a parameter which allows us to pass variable length argument list to the **`adder()`** function. Inside the function, we have a loop which adds the passed argument and prints the result. We passed 3 different tuples with variable length as an argument to the function." |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "markdown", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "## Python __`**kwargs`__\n", |
| 190 | + "\n", |
| 191 | + "Python passes variable length non keyword argument to function using __`*args`__ but we cannot use this to pass keyword argument. For this problem Python has got a solution called __`**kwargs`__, it allows us to pass the variable length of keyword arguments to the function.\n", |
| 192 | + "\n", |
| 193 | + "In the function, we use the double asterisk __`**`__ before the parameter name to denote this type of argument. The arguments are passed as a dictionary and these arguments make a dictionary inside function with name same as the parameter excluding double asterisk __`**`__." |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 4, |
| 199 | + "metadata": { |
| 200 | + "ExecuteTime": { |
| 201 | + "end_time": "2021-07-31T13:08:57.897046Z", |
| 202 | + "start_time": "2021-07-31T13:08:57.871652Z" |
| 203 | + }, |
| 204 | + "scrolled": true |
| 205 | + }, |
| 206 | + "outputs": [ |
| 207 | + { |
| 208 | + "name": "stdout", |
| 209 | + "output_type": "stream", |
| 210 | + "text": [ |
| 211 | + "\n", |
| 212 | + "Data type of argument: <class 'dict'>\n", |
| 213 | + "Firstname is Amy\n", |
| 214 | + "Lastname is Barn\n", |
| 215 | + "Age is 24\n", |
| 216 | + "Phone is 1234567890\n", |
| 217 | + "\n", |
| 218 | + "Data type of argument: <class 'dict'>\n", |
| 219 | + "Firstname is Arthur\n", |
| 220 | + "Lastname is Hunt\n", |
| 221 | + |
| 222 | + "Country is Atlantis\n", |
| 223 | + "Age is 27\n", |
| 224 | + "Phone is 9976563219\n" |
| 225 | + ] |
| 226 | + } |
| 227 | + ], |
| 228 | + "source": [ |
| 229 | + "# Example 3: Using **kwargs to pass the variable keyword arguments to the function \n", |
| 230 | + "\n", |
| 231 | + "def intro(**data):\n", |
| 232 | + " print(\"\\nData type of argument:\",type(data))\n", |
| 233 | + "\n", |
| 234 | + " for key, value in data.items():\n", |
| 235 | + " print(\"{} is {}\".format(key,value))\n", |
| 236 | + "\n", |
| 237 | + "intro(Firstname=\"Amy\", Lastname=\"Barn\", Age=24, Phone=1234567890)\n", |
| 238 | + "intro(Firstname=\"Arthur\", Lastname=\"Hunt\", Email=\"[email protected]\", Country=\"Atlantis\", Age=27, Phone=9976563219)" |
| 239 | + ] |
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "markdown", |
| 243 | + "metadata": {}, |
| 244 | + "source": [ |
| 245 | + "**Explanation:**\n", |
| 246 | + "\n", |
| 247 | + "In the above program, we have a function **`intro()`** with __`**data`__ as a parameter. We passed two dictionaries with variable argument length to the **`intro()`** function. We have for loop inside **`intro()`** function which works on the data of passed dictionary and prints the value of the dictionary.." |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "markdown", |
| 252 | + "metadata": {}, |
| 253 | + "source": [ |
| 254 | + "## Things to Remember:\n", |
| 255 | + "\n", |
| 256 | + "* __`*args`__ and __`**kwargs`__ are special keyword which allows function to take variable length argument.\n", |
| 257 | + "* __`*args`__ passes variable number of non-keyworded arguments list and on which operation of the list can be performed.\n", |
| 258 | + "* __`**kwargs`__ passes variable number of keyword arguments dictionary to function on which operation of a dictionary can be performed.\n", |
| 259 | + "* __`*args`__ and __`**kwargs`__ make the function flexible." |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": null, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [], |
| 267 | + "source": [] |
| 268 | + } |
| 269 | + ], |
| 270 | + "metadata": { |
| 271 | + "hide_input": false, |
| 272 | + "kernelspec": { |
| 273 | + "display_name": "Python 3", |
| 274 | + "language": "python", |
| 275 | + "name": "python3" |
| 276 | + }, |
| 277 | + "language_info": { |
| 278 | + "codemirror_mode": { |
| 279 | + "name": "ipython", |
| 280 | + "version": 3 |
| 281 | + }, |
| 282 | + "file_extension": ".py", |
| 283 | + "mimetype": "text/x-python", |
| 284 | + "name": "python", |
| 285 | + "nbconvert_exporter": "python", |
| 286 | + "pygments_lexer": "ipython3", |
| 287 | + "version": "3.8.8" |
| 288 | + }, |
| 289 | + "toc": { |
| 290 | + "base_numbering": 1, |
| 291 | + "nav_menu": {}, |
| 292 | + "number_sections": true, |
| 293 | + "sideBar": true, |
| 294 | + "skip_h1_title": false, |
| 295 | + "title_cell": "Table of Contents", |
| 296 | + "title_sidebar": "Contents", |
| 297 | + "toc_cell": false, |
| 298 | + "toc_position": {}, |
| 299 | + "toc_section_display": true, |
| 300 | + "toc_window_display": false |
| 301 | + }, |
| 302 | + "varInspector": { |
| 303 | + "cols": { |
| 304 | + "lenName": 16, |
| 305 | + "lenType": 16, |
| 306 | + "lenVar": 40 |
| 307 | + }, |
| 308 | + "kernels_config": { |
| 309 | + "python": { |
| 310 | + "delete_cmd_postfix": "", |
| 311 | + "delete_cmd_prefix": "del ", |
| 312 | + "library": "var_list.py", |
| 313 | + "varRefreshCmd": "print(var_dic_list())" |
| 314 | + }, |
| 315 | + "r": { |
| 316 | + "delete_cmd_postfix": ") ", |
| 317 | + "delete_cmd_prefix": "rm(", |
| 318 | + "library": "var_list.r", |
| 319 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 320 | + } |
| 321 | + }, |
| 322 | + "types_to_exclude": [ |
| 323 | + "module", |
| 324 | + "function", |
| 325 | + "builtin_function_or_method", |
| 326 | + "instance", |
| 327 | + "_Feature" |
| 328 | + ], |
| 329 | + "window_display": false |
| 330 | + } |
| 331 | + }, |
| 332 | + "nbformat": 4, |
| 333 | + "nbformat_minor": 4 |
| 334 | +} |
0 commit comments