{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sub1 g: 3\n",
      "sub1 a: 5\n",
      "sub1 b: 7\n",
      "sub2 g: 5\n",
      "sub2 c: 9\n",
      "sub3 g: 11\n",
      "sub3 c: 9\n",
      "after: 5\n"
     ]
    }
   ],
   "source": [
    "g = 3\n",
    "def sub1():\n",
    "    a = 5\n",
    "    b = 7\n",
    "    print(\"sub1 g:\", g)\n",
    "    print(\"sub1 a:\", a)\n",
    "    print(\"sub1 b:\", b)\n",
    "    def sub2():\n",
    "        global g\n",
    "        c = 9\n",
    "        g = 5                  # change global val of g\n",
    "        print(\"sub2 g:\", g)\n",
    "        print(\"sub2 c:\", c)\n",
    "        def sub3():\n",
    "                nonlocal c\n",
    "                g=11\n",
    "                print(\"sub3 g:\", g)\n",
    "                print(\"sub3 c:\", c)\n",
    "        sub3()        \n",
    "    sub2()\n",
    "sub1()\n",
    "print(\"after:\", g)             # global value of g changed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
