{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modules\n", "- module is a file containing Python definitions and statements intended for use in other Python programs\n", "- many Python modules come with built-in standard library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Various ways to import names into the current namespace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import math module into the global namespace\n", "import math\n", "x = math.sqrt(100)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "print(random.choice(list(range(1, 21))))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import choice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(choice([1, 2, 3, 4]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(math)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import * # Import all the identifiers from math\n", "print(sqrt(100))\n", "print(pi)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import radians, sin\n", "rad = radians(90)\n", "print(rad)\n", "print(sin(rad))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## names can be imported to local namespace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def isUpper(letter):\n", " import string # string name is local\n", " return letter in string.ascii_uppercase" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(isUpper('a'))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'string' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\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[1;32m 1\u001b[0m \u001b[0;31m# can we use string module outside isUpper function?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstring\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdigits\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'string' is not defined" ] } ], "source": [ "# can we use string module outside isUpper function?\n", "print(string.digits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## scope and lookup rules\n", "

The scope of an identifier is the region of program code in which the identifier can be accessed, or used.

\n", "Three important scopes in Python:\n", "- Local scope refers to identifiers declared within a function\n", "- Global scope refers to all the identifiers declared within the current module, or file\n", "- Built-in scope refers to all the identifiers built into Python -- those like range and min that are (almost) always available \n", "

Precedence rule:

\n", "
    \n", "
  1. innermost or local scope
  2. \n", "
  3. global scope
  4. \n", "
  5. built-in scope
  6. \n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def testLocalScope():\n", " k = 5\n", " for i in range(2):\n", " for j in range(2):\n", " if i == j:\n", " k = 3\n", " print('inside=',k)\n", " print('outside=',k)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "inside= 3\n", "inside= 3\n", "outside= 3\n" ] } ], "source": [ "test()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'k' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'k' is not defined" ] } ], "source": [ "print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## User-defined modules\n", "#### use module1.py, module2.py inside modules folder to demonstrate user defined modules and importance of:\n", "```if __name__ == '__main__':\n", " ...\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Packages\n", "- folder with module(s)\n", "- must define \\__init\\__.py empty module to initialize as package\n", "- can't import package itself (in a useful way) but only module(s) or identifiers in the modules\n", "- https://docs.python.org/3/tutorial/modules.html#packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## use fibos package to demostrate user-defined package" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import fibos" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on package fibos:\n", "\n", "NAME\n", " fibos\n", "\n", "PACKAGE CONTENTS\n", " fibo\n", "\n", "FILE\n", " /Volumes/Storage/CMU/Sp2019/CS0/thinkpythonnotebooks/fibos/__init__.py\n", "\n", "\n" ] } ], "source": [ "help(fibos)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'fibos' has no attribute 'fibo'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\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[0mfibos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfibo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfib\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: module 'fibos' has no attribute 'fibo'" ] } ], "source": [ "fibos.fibo.fib(10)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1 1 2 3 5 8 13 21 34 \n" ] } ], "source": [ "import fibos.fibo as f\n", "f.fib(10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1 1 2 3 5 8 13 21 34 \n" ] } ], "source": [ "from fibos import fibo\n", "fibo.fib(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }