{ "metadata": { "name": "", "signature": "sha256:82f2ca89f77322e9e4a61c2a9d3a9958fbc8f72b5fc7ec07f05d21fbabe00167" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Global and local scope of Python variables" ] }, { "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": [ "Variable *scope* refers to the places that you can see or access a variable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you define a variable at the top level of your script or module or notebook, this is a global variable:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_var = 3" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable is *global* because any Python function or class defined in this module or notebook, is able to access this variable. For example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def my_first_func():\n", " # my_func can 'see' the global variable\n", " print('I see \"my_var\" = ', my_var, ' from \"my_first_func\"')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "my_first_func()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I see \"my_var\" = 3 from \"my_first_func\"\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables defined inside a function or class, are not global. Only the function or class can see the variable:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def my_second_func():\n", " a = 10\n", " print('I see \"a\" = ', a, 'from \"my_second_func\"')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "my_second_func()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I see \"a\" = 10 from \"my_second_func\"\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But here, down in the top or global level of the notebook, we can't see that variable:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'a' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable `a` is therefore said to be *local* to the function. Put another way, the variable `a` has local scope. Conversely the variable `my_var` has global scope." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The rules on variable scope cover many more cases than these very simple examples.\n", "\n", "See http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/scope_resolution_legb_rule.ipynb for a nice walkthrough." ] } ], "metadata": {} } ] }