{ "metadata": { "name": "", "signature": "sha256:8e2def4905c10374a9990f1a9fc522a4b7e7b50d8d59b72ee544b7190a89e6b5" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python and its Functions\n", "> Shrayas (@shrayasr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **Note:** Won't spend too much time on the body of the functions unless required" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Defining functions\n", "> `def`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foobar():\n", " print \"Foobar\"\n", " \n", "foobar()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Foobar\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Passing Arguments to functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def say_hello(name, language):\n", "\n", " language_responses = {\n", " \"ENGLISH\": \"Hello, \",\n", " \"JAPANESE\": \"Konichiwa, \",\n", " }\n", "\n", " # Clean the input\n", " language = language.strip().upper()\n", " hello_str = language_responses.get(language)\n", "\n", " if hello_str:\n", " return hello_str + name\n", " else:\n", " return \"Language not supported\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Positional arguments\n", "> Arguments passed one after another" ] }, { "cell_type": "code", "collapsed": false, "input": [ "say_hello(\"Chennaipy\", \"English\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "'Hello, Chennaipy'" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "say_hello(\"English\", \"Chennaipy\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "'Language not supported'" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "_Which of course means, the order in which they are passed, matters_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Keyword Arguments \n", "> Arguments passed into the function with a keyword" ] }, { "cell_type": "code", "collapsed": false, "input": [ "say_hello(name=\"Chennaipy\", language=\"Japanese\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "'Konichiwa, Chennaipy'" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "say_hello(language=\"Japanese\", name=\"Chennaipy\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "'Konichiwa, Chennaipy'" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "_Which of course means, the order in which they are passed, **doesn't** matter_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variadic functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### *\n", "> Positional" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def square(*nums):\n", " print type(nums)\n", " print nums\n", " \n", "square(2,3,4,6,7,8,5,4,3,2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "(2, 3, 4, 6, 7, 8, 5, 4, 3, 2)\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ** \n", "> Keyword" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def make_coffee(**config):\n", " print type(config)\n", " print config\n", " \n", "make_coffee(decaf=False, milk=True, extra_sugar=True)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "{'decaf': False, 'extra_sugar': True, 'milk': True}\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mix and Match\n", "> Because, Python yo!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def illustrate_pos_kw_args(first_arg, *args, **kwargs):\n", " print first_arg\n", " print args\n", " print kwargs\n", " \n", "illustrate_pos_kw_args(1,2,3,a=1, b=2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "(2, 3)\n", "{'a': 1, 'b': 2}\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Default arguments to functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def hello_world(name=None):\n", " if name:\n", " return \"Hello, \" + name\n", " else:\n", " return \"Hello, world\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "print hello_world()\n", "print hello_world(\"Chennaipy\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello, world\n", "Hello, Chennaipy\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Defining Functions inside Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def make_dosa(masala=False, red_chutney=False, extra_roast=False):\n", " \n", " def _evaluate_dosa_configuration(masala, red_chutney, extra_roast):\n", " print masala\n", " print red_chutney\n", " print extra_roast\n", " return True\n", " \n", " def _make_dosa(masala, red_chutney, extra_roast):\n", " return \" ______\\n(______)\"\n", " \n", " if _evaluate_dosa_configuration(masala, red_chutney, extra_roast):\n", " return _make_dosa(masala, red_chutney, extra_roast)\n", " \n", "print make_dosa(masala=True)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "False\n", "False\n", " ______\n", "(______)\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lexical Scoping in functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def fn1(arg1, arg2, arg3):\n", " \n", " def fn2():\n", " print \"from fn2\", arg1, arg2, arg3\n", " \n", " fn2()\n", "\n", "fn1(1,2,3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "from fn2 1 2 3\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "def make_dosa(masala=False, red_chutney=False, extra_roast=False):\n", " \n", " def _evaluate_dosa_configuration():\n", " print masala\n", " print red_chutney\n", " print extra_roast\n", " return True\n", " \n", " def _make_dosa():\n", " return \" ______\\n(______)\"\n", " \n", " if _evaluate_dosa_configuration():\n", " return _make_dosa()\n", " \n", "print make_dosa(masala=True)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "False\n", "False\n", " ______\n", "(______)\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Passing Functions as Arguments to Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def make_dosa(masala=False, red_chutney=False, extra_roast=False, \\\n", " method_of_preparation=None):\n", " \n", " if not method_of_preparation:\n", " raise Exception(\"Don't know how to make dosa\")\n", " \n", " def _evaluate_dosa_configuration():\n", " return True\n", " \n", " def _make_dosa():\n", " return method_of_preparation()\n", " \n", " if _evaluate_dosa_configuration():\n", " return _make_dosa()\n", " \n", "def chennai_dosa():\n", " return \" ___________\\n(__CHENNAI__)\"\n", "\n", "def bangalore_dosa():\n", " return \" ___________\\n(_BANGALORE_)\"\n", "\n", " \n", "print make_dosa(masala=True, method_of_preparation=chennai_dosa)\n", "print make_dosa(masala=True, method_of_preparation=bangalore_dosa)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " ___________\n", "(__CHENNAI__)\n", " ___________\n", "(_BANGALORE_)\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "chennai_dosa" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "chennai_dosa.__class__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "function" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Returning Functions from Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dosa_maker(style):\n", " \n", " def chennai(*args, **kwargs):\n", " print args, kwargs\n", " return \"Suda suda dosa saar\"\n", " \n", " def bangalore(*args, **kwargs):\n", " print args, kwargs\n", " return \"Bisi Bisi dose saar\"\n", " \n", " styles = {\n", " \"CHENNAI\": chennai,\n", " \"BANGALORE\": bangalore\n", " }\n", " \n", " style = style.strip().upper()\n", " return styles[style]\n", "\n", "bangalore_dosa_maker = dosa_maker(style=\"bangalore\")\n", "print bangalore_dosa_maker(masala=True)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "() {'masala': True}\n", "Bisi Bisi dose saar\n" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion\n", "\n", "* Functions in Python are awesome\n", "* You can: \n", " * Define functions inside functions\n", " * Pass functions as arguments to other functions\n", " * Return functions from functions\n", "* Functions in Python are First Class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Fin." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![First class!](./Logo.png)" ] } ], "metadata": {} } ] }