|
1 | 1 | {
|
2 |
| - "cells": [], |
3 |
| - "metadata": {}, |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Args and Kwargs" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "def test_var_args(f_arg, *argv):\n", |
| 17 | + " print(\"first normal arg:\", f_arg)\n", |
| 18 | + " for arg in argv:\n", |
| 19 | + " print(\"another arg through *argv :\", arg)\n", |
| 20 | + "\n", |
| 21 | + "test_var_args('yasoob','python','eggs','test')" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "### **kwargs allows you to pass keyworded variable length of arguments to a function. \n", |
| 29 | + "\n", |
| 30 | + "### You should use **kwargs if you want to handle named arguments in a function" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "def greet_me(**kwargs):\n", |
| 40 | + " if kwargs is not None:\n", |
| 41 | + " for key, value in kwargs.items():\n", |
| 42 | + " print(\"{} == {}\".format(key,value))\n", |
| 43 | + "\n", |
| 44 | + "greet_me(name=\"Rishabh\")" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "def test_args_kwargs(arg1, arg2, arg3):\n", |
| 54 | + " print(\"arg1:\"), arg1\n", |
| 55 | + " print(\"arg2:\"), arg2\n", |
| 56 | + " print(\"arg3:\"), arg3" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "args = (\"two\", 3,5)\n", |
| 66 | + "test_args_kwargs(*args)" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "kwargs = {\"arg3\": 3, \"arg2\": \"two\",\"arg1\":5}\n", |
| 76 | + "test_args_kwargs(**kwargs)" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "def some_func(fargs,*args,**kwargs):\n", |
| 86 | + " ..." |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": null, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [] |
| 95 | + } |
| 96 | + ], |
| 97 | + "metadata": { |
| 98 | + "kernelspec": { |
| 99 | + "display_name": "Python 3", |
| 100 | + "language": "python", |
| 101 | + "name": "python3" |
| 102 | + }, |
| 103 | + "language_info": { |
| 104 | + "codemirror_mode": { |
| 105 | + "name": "ipython", |
| 106 | + "version": 3 |
| 107 | + }, |
| 108 | + "file_extension": ".py", |
| 109 | + "mimetype": "text/x-python", |
| 110 | + "name": "python", |
| 111 | + "nbconvert_exporter": "python", |
| 112 | + "pygments_lexer": "ipython3", |
| 113 | + "version": "3.6.8" |
| 114 | + } |
| 115 | + }, |
4 | 116 | "nbformat": 4,
|
5 | 117 | "nbformat_minor": 2
|
6 | 118 | }
|
0 commit comments