|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "7934a87e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# args & kwargs\n", |
| 9 | + "## *args\n", |
| 10 | + "When we are not sure about the number of arguments being passed to a function then we can\n", |
| 11 | + "use *args as function parameter.\n", |
| 12 | + "*args allow us to pass the variable number of Non Keyword Arguments to function.\n", |
| 13 | + "We can simply use an asterisk * before the parameter name to pass variable length\n", |
| 14 | + "arguments.\n", |
| 15 | + "The arguments are always passed as a tuple.\n", |
| 16 | + "We can rename it to anything as long as it is preceded by a single asterisk (*). It's best\n", |
| 17 | + "practice to keep naming it args to make it immediately recognizable.\n", |
| 18 | + "## **kwargs\n", |
| 19 | + "**kwargs allows us to pass the variable number of Keyword Arguments to the function.\n", |
| 20 | + "We can simply use an double asterisk ** before the parameter name to pass variable length\n", |
| 21 | + "arguments.\n", |
| 22 | + "The arguments are passed as a dictionary.\n", |
| 23 | + "We can rename it to anything as long as it is preceded by a double asterisk (**). It's best\n", |
| 24 | + "practice to keep naming it kwargs to make it immediately recognizable" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 1, |
| 30 | + "id": "c8abaf21", |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stdout", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + "60\n" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
| 41 | + "source": [ |
| 42 | + "def add(a,b,c):\n", |
| 43 | + " return a+b+c\n", |
| 44 | + " \n", |
| 45 | + "print(add(10,20,30)) # Sum of two numbers" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 2, |
| 51 | + "id": "2161cf58", |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "ename": "TypeError", |
| 56 | + "evalue": "add() takes 3 positional arguments but 4 were given", |
| 57 | + "output_type": "error", |
| 58 | + "traceback": [ |
| 59 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 60 | + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 61 | + "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43madd\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m)\u001b[49m) \n\u001b[0;32m 2\u001b[0m \u001b[38;5;124;03m'''This will throw below error as this function will only take\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124;03mIf we want to make argument list dynamic then *args wil come in picture'''\u001b[39;00m\n", |
| 62 | + "\u001b[1;31mTypeError\u001b[0m: add() takes 3 positional arguments but 4 were given" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "print(add(1,2,3,4)) \n", |
| 68 | + "'''This will throw below error as this function will only take\n", |
| 69 | + "If we want to make argument list dynamic then *args wil come in picture'''" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 4, |
| 75 | + "id": "810bc714", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "arg_1: 1\n", |
| 83 | + "arg_2: 2\n", |
| 84 | + "arg_3: 3\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "def some_args(arg_1, arg_2, arg_3):\n", |
| 90 | + " print(\"arg_1:\", arg_1)\n", |
| 91 | + " print(\"arg_2:\", arg_2)\n", |
| 92 | + " print(\"arg_3:\", arg_3)\n", |
| 93 | + "\n", |
| 94 | + "my_list = [2, 3]\n", |
| 95 | + "some_args(1, *my_list)" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 7, |
| 101 | + "id": "2570a490", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "name": "stdout", |
| 106 | + "output_type": "stream", |
| 107 | + "text": [ |
| 108 | + "6\n", |
| 109 | + "10\n", |
| 110 | + "15\n", |
| 111 | + "21\n", |
| 112 | + "28\n" |
| 113 | + ] |
| 114 | + } |
| 115 | + ], |
| 116 | + "source": [ |
| 117 | + "def add1(*args):\n", |
| 118 | + " return sum(args)\n", |
| 119 | + "print(add1(1,2,3))\n", |
| 120 | + "print(add1(1,2,3,4)) # *args will take dynamic argument list.\n", |
| 121 | + "print(add1(1,2,3,4,5))\n", |
| 122 | + "print(add1(1,2,3,4,5,6))\n", |
| 123 | + "print(add1(1,2,3,4,5,6,7))" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 8, |
| 129 | + "id": "f252e4d4", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "data": { |
| 134 | + "text/plain": [ |
| 135 | + "(28, 28)" |
| 136 | + ] |
| 137 | + }, |
| 138 | + "execution_count": 8, |
| 139 | + "metadata": {}, |
| 140 | + "output_type": "execute_result" |
| 141 | + } |
| 142 | + ], |
| 143 | + "source": [ |
| 144 | + "list1 = [1,2,3,4,5,6,7]\n", |
| 145 | + "tuple1 = (1,2,3,4,5,6,7)\n", |
| 146 | + "add1(*list1) , add1(*tuple1) #tuple & list items will be passed as argument list" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": 9, |
| 152 | + "id": "7c35418f", |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [ |
| 155 | + { |
| 156 | + "data": { |
| 157 | + "text/plain": [ |
| 158 | + "112" |
| 159 | + ] |
| 160 | + }, |
| 161 | + "execution_count": 9, |
| 162 | + "metadata": {}, |
| 163 | + "output_type": "execute_result" |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "list1 = [1,2,3,4,5,6,7]\n", |
| 168 | + "list2 = [1,2,3,4,5,6,7]\n", |
| 169 | + "list3 = [1,2,3,4,5,6,7]\n", |
| 170 | + "list4 = [1,2,3,4,5,6,7]\n", |
| 171 | + "add1(*list1 , *list2 , *list3 , *list4 )" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 10, |
| 177 | + "id": "64728bc9", |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "name": "stdout", |
| 182 | + "output_type": "stream", |
| 183 | + "text": [ |
| 184 | + "('Sunil', 9421, 416311, 39, 'India', 'Maharashtra', 'Marathi')\n" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "data": { |
| 189 | + "text/plain": [ |
| 190 | + "' For the above example we have no idea about the parameters passed e.g 9421 , \\n In such cases we can take help of Keyworded arguments (**kwargs) '" |
| 191 | + ] |
| 192 | + }, |
| 193 | + "execution_count": 10, |
| 194 | + "metadata": {}, |
| 195 | + "output_type": "execute_result" |
| 196 | + } |
| 197 | + ], |
| 198 | + "source": [ |
| 199 | + "def UserDetails(*args):\n", |
| 200 | + " print(args)\n", |
| 201 | + "UserDetails('Sunil' , 9421 , 416311 , 39 , 'India' , 'Maharashtra', 'Marathi')\n", |
| 202 | + "''' For the above example we have no idea about the parameters passed e.g 9421 , \n", |
| 203 | + " In such cases we can take help of Keyworded arguments (**kwargs) '''" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": 11, |
| 209 | + "id": "ea95f11f", |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [ |
| 212 | + { |
| 213 | + "name": "stdout", |
| 214 | + "output_type": "stream", |
| 215 | + "text": [ |
| 216 | + "Name :- Sunil\n", |
| 217 | + "ID :- 9421\n", |
| 218 | + "Pincode :- 416311\n", |
| 219 | + "Age :- 39\n", |
| 220 | + "Country :- India\n", |
| 221 | + "State :- Maharashtra\n", |
| 222 | + "Language :- Marathi\n" |
| 223 | + ] |
| 224 | + } |
| 225 | + ], |
| 226 | + "source": [ |
| 227 | + "def UserDetails(**kwargs):\n", |
| 228 | + " for key,val in kwargs.items():\n", |
| 229 | + " print(\"{} :- {}\".format(key,val))\n", |
| 230 | + "UserDetails(Name='Sunil' , ID=9421 , Pincode=416311 , Age= 39 , \n", |
| 231 | + " Country= 'India', State='Maharashtra',Language='Marathi')" |
| 232 | + ] |
| 233 | + }, |
| 234 | + { |
| 235 | + "cell_type": "code", |
| 236 | + "execution_count": 12, |
| 237 | + "id": "a75a3496", |
| 238 | + "metadata": {}, |
| 239 | + "outputs": [], |
| 240 | + "source": [ |
| 241 | + "mydict = {'Name': 'Sunil', 'ID': 9970, 'Pincode': 412213, 'Age': 40, \n", |
| 242 | + " 'Country': 'India', 'State' :'Maharashtra','Language':'Marathi'}" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "code", |
| 247 | + "execution_count": 13, |
| 248 | + "id": "b7cc3f83", |
| 249 | + "metadata": {}, |
| 250 | + "outputs": [ |
| 251 | + { |
| 252 | + "name": "stdout", |
| 253 | + "output_type": "stream", |
| 254 | + "text": [ |
| 255 | + "Name :- Sunil\n", |
| 256 | + "ID :- 9970\n", |
| 257 | + "Pincode :- 412213\n", |
| 258 | + "Age :- 40\n", |
| 259 | + "Country :- India\n", |
| 260 | + "State :- Maharashtra\n", |
| 261 | + "Language :- Marathi\n" |
| 262 | + ] |
| 263 | + } |
| 264 | + ], |
| 265 | + "source": [ |
| 266 | + "UserDetails(**mydict)" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": 20, |
| 272 | + "id": "0dfcd883", |
| 273 | + "metadata": {}, |
| 274 | + "outputs": [ |
| 275 | + { |
| 276 | + "name": "stdout", |
| 277 | + "output_type": "stream", |
| 278 | + "text": [ |
| 279 | + "License No :- MH-10\n", |
| 280 | + "Full Name :- Sunil Zambare Dongarsoni\n", |
| 281 | + "Phone Number:- 12345678910\n", |
| 282 | + "Name :- Sunil\n", |
| 283 | + "ID :- 9970\n", |
| 284 | + "Pincode :- 416311\n", |
| 285 | + "Age :- 40\n", |
| 286 | + "Village :- Ayodhyanagar\n", |
| 287 | + "State :- Maharashtra\n", |
| 288 | + "Country :- India\n" |
| 289 | + ] |
| 290 | + } |
| 291 | + ], |
| 292 | + "source": [ |
| 293 | + "def UserDetails(licenseNo, *args , phoneNo=0 , **kwargs): # Using all four argum\n", |
| 294 | + " print('License No :- ', licenseNo)\n", |
| 295 | + " j=''\n", |
| 296 | + " for i in args:\n", |
| 297 | + " j = j+i\n", |
| 298 | + " print('Full Name :-',j)\n", |
| 299 | + " print('Phone Number:- ',phoneNo)\n", |
| 300 | + " for key,val in kwargs.items():\n", |
| 301 | + " print(\"{} :- {}\".format(key,val))\n", |
| 302 | + "\n", |
| 303 | + "name = ['Sunil' , ' ' , 'Zambare' , ' ','Dongarsoni']\n", |
| 304 | + "mydict = {'Name': 'Sunil', 'ID': 9970, 'Pincode': 416311, 'Age': 40, \n", |
| 305 | + " 'Village' :'Ayodhyanagar','State' :'Maharashtra', 'Country': 'India'}\n", |
| 306 | + "UserDetails('MH-10' , *name , phoneNo=12345678910,**mydict )" |
| 307 | + ] |
| 308 | + }, |
| 309 | + { |
| 310 | + "cell_type": "code", |
| 311 | + "execution_count": 21, |
| 312 | + "id": "8d58bd2e", |
| 313 | + "metadata": {}, |
| 314 | + "outputs": [], |
| 315 | + "source": [ |
| 316 | + "def UserDetails(licenseNo, *args , phoneNo=0, **kwargs): # Using all four arguments\n", |
| 317 | + " print('Nothing')" |
| 318 | + ] |
| 319 | + }, |
| 320 | + { |
| 321 | + "cell_type": "code", |
| 322 | + "execution_count": 22, |
| 323 | + "id": "2e7734e4", |
| 324 | + "metadata": {}, |
| 325 | + "outputs": [ |
| 326 | + { |
| 327 | + "ename": "SyntaxError", |
| 328 | + "evalue": "invalid syntax (3335583366.py, line 1)", |
| 329 | + "output_type": "error", |
| 330 | + "traceback": [ |
| 331 | + "\u001b[1;36m Cell \u001b[1;32mIn[22], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m def UserDetails(licenseNo, **kwargs , *args): # This will fail.\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" |
| 332 | + ] |
| 333 | + } |
| 334 | + ], |
| 335 | + "source": [ |
| 336 | + "def UserDetails(licenseNo, **kwargs , *args): # This will fail.\n", |
| 337 | + " print('Nothing')" |
| 338 | + ] |
| 339 | + }, |
| 340 | + { |
| 341 | + "cell_type": "code", |
| 342 | + "execution_count": null, |
| 343 | + "id": "03b621a6", |
| 344 | + "metadata": {}, |
| 345 | + "outputs": [], |
| 346 | + "source": [] |
| 347 | + } |
| 348 | + ], |
| 349 | + "metadata": { |
| 350 | + "kernelspec": { |
| 351 | + "display_name": "Python 3 (ipykernel)", |
| 352 | + "language": "python", |
| 353 | + "name": "python3" |
| 354 | + }, |
| 355 | + "language_info": { |
| 356 | + "codemirror_mode": { |
| 357 | + "name": "ipython", |
| 358 | + "version": 3 |
| 359 | + }, |
| 360 | + "file_extension": ".py", |
| 361 | + "mimetype": "text/x-python", |
| 362 | + "name": "python", |
| 363 | + "nbconvert_exporter": "python", |
| 364 | + "pygments_lexer": "ipython3", |
| 365 | + "version": "3.9.12" |
| 366 | + } |
| 367 | + }, |
| 368 | + "nbformat": 4, |
| 369 | + "nbformat_minor": 5 |
| 370 | +} |
0 commit comments