{ "metadata": { "name": "", "signature": "sha256:5c59278ce707ad820da14234d1d10e75145e80540a1d8ef1af089493846c86fd" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# In Python, functions are objects too" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - compatibility with Python 3\n", "from __future__ import print_function # print('me') instead of print 'me'\n", "from __future__ import division # 1/2 == 0.5, not 0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If I make a string in Python:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = 'Matthew'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "then I have a Python object of type `str`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(name)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "str" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say I defined a function `add`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(a, b):\n", " return a + b" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now I have another Python object, of type `function`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(add)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "function" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "With my string, I can refer to the same string object, with a different variable name:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "prisoner = name\n", "prisoner" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "'Matthew'" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's the same for functions, because they are objects too:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_add = add\n", "type(my_add)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "function" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions are objects you can \"call\" by following the function object name with parentheses and arguments you want to pass:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "add(1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "3" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "my_add(1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "3" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As for any other object in Python, you can pass function objects to other functions: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def run_a_func(func, arg1, arg2):\n", " result = func(arg1, arg2)\n", " print('Result was', result)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "run_a_func(add, 1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Result was 3\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "run_a_func(my_add, 1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Result was 3\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "def sub(a, b):\n", " return a - b" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "run_a_func(sub, 1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Result was -1\n" ] } ], "prompt_number": 14 } ], "metadata": {} } ] }