diff --git a/Intermediate.ipynb b/Intermediate.ipynb index f3207bb..6edb3ef 100644 --- a/Intermediate.ipynb +++ b/Intermediate.ipynb @@ -3961,29 +3961,33 @@ }, { "cell_type": "markdown", - "id": "9e1c43a8", + "id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e", "metadata": { + "editable": false, "slideshow": { - "slide_type": "slide" + "slide_type": "" }, - "tags": [], - "editable": false + "tags": [] }, "source": [ - "## Importing _modules_" + "Modules let us:\n", + "- Use code that others have already written and tested\n", + "- Load in only the functionality we need\n", + "- Know exactly where a piece of functionality comes from" ] }, { "cell_type": "markdown", - "id": "eed64626", + "id": "9e1c43a8", "metadata": { "slideshow": { - "slide_type": "" + "slide_type": "slide" }, + "tags": [], "editable": false }, "source": [ - "Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet." + "## Importing _modules_" ] }, { @@ -3993,6 +3997,7 @@ "slideshow": { "slide_type": "" }, + "tags": [], "editable": false }, "source": [ @@ -4087,6 +4092,71 @@ "print(square_root(25))" ] }, + { + "cell_type": "markdown", + "id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## Why Namespaces Matter\n", + "\n", + "The word `log` means two very different things:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4de72e4c-2795-4b4d-8542-10729a74f44a", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import logging\n", + "\n", + "math.log(100, 10) # A logarithm: 2.0\n", + "\n", + "logging.log(20, \"Hello\") # A logbook entry: records a message" + ] + }, + { + "cell_type": "markdown", + "id": "36f46ead-b9a1-481f-8798-edbfc58b5504", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "Without namespaces, Python, and your reader, couldn't tell them apart." + ] + }, + { + "cell_type": "markdown", + "id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "fragment" + }, + "tags": [] + }, + "source": [ + "----\n", + "This is why importing a whole module into namespace is usually a bad idea:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -4099,10 +4169,9 @@ }, "outputs": [], "source": [ - "# import all functions, variables, and classes from a module into the global namespace\n", - "# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n", + "# import all functions, variables, and classes from a module - better to avoid this\n", "from math import *\n", - "print(int(sqrt(4.0)))" + "print(sqrt(4.0))" ] }, { @@ -4110,13 +4179,13 @@ "id": "d1cc0855", "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "slide" }, "tags": [], "editable": false }, "source": [ - "---\n", + "## Getting help\n", "To get help on a module at the Python shell, import it the whole (the very first way), then you can..." ] }, diff --git a/Intermediate_full.ipynb b/Intermediate_full.ipynb index 453486f..ed93204 100644 --- a/Intermediate_full.ipynb +++ b/Intermediate_full.ipynb @@ -4077,27 +4077,32 @@ }, { "cell_type": "markdown", - "id": "9e1c43a8", + "id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e", "metadata": { + "editable": true, "slideshow": { - "slide_type": "slide" + "slide_type": "" }, "tags": [] }, "source": [ - "## Importing _modules_" + "Modules let us:\n", + "- Use code that others have already written and tested\n", + "- Load in only the functionality we need\n", + "- Know exactly where a piece of functionality comes from" ] }, { "cell_type": "markdown", - "id": "eed64626", + "id": "9e1c43a8", "metadata": { "slideshow": { - "slide_type": "" - } + "slide_type": "slide" + }, + "tags": [] }, "source": [ - "Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet." + "## Importing _modules_" ] }, { @@ -4106,7 +4111,8 @@ "metadata": { "slideshow": { "slide_type": "" - } + }, + "tags": [] }, "source": [ "There are several ways of importing _modules_:" @@ -4200,6 +4206,86 @@ "print(square_root(25))" ] }, + { + "cell_type": "markdown", + "id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## Why Namespaces Matter\n", + "\n", + "The word `log` means two very different things:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4de72e4c-2795-4b4d-8542-10729a74f44a", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import logging\n", + "\n", + "math.log(100, 10) # A logarithm: 2.0\n", + "\n", + "logging.log(20, \"Hello\") # A logbook entry: records a message" + ] + }, + { + "cell_type": "markdown", + "id": "36f46ead-b9a1-481f-8798-edbfc58b5504", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "Without namespaces, Python, and your reader, couldn't tell them apart." + ] + }, + { + "cell_type": "markdown", + "id": "ccbef34a-9226-4735-859d-378640ee9cc7", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "notes" + }, + "tags": [] + }, + "source": [ + "Notes:\n", + "- In JupyterLite, logging output is suppressed by default unless you explicitly configure a handler." + ] + }, + { + "cell_type": "markdown", + "id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "fragment" + }, + "tags": [] + }, + "source": [ + "----\n", + "This is why importing a whole module into namespace is usually a bad idea:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -4212,10 +4298,9 @@ }, "outputs": [], "source": [ - "# import all functions, variables, and classes from a module into the global namespace\n", - "# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n", + "# import all functions, variables, and classes from a module - better to avoid this\n", "from math import *\n", - "print(int(sqrt(4.0)))" + "print(sqrt(4.0))" ] }, { @@ -4223,12 +4308,12 @@ "id": "d1cc0855", "metadata": { "slideshow": { - "slide_type": "fragment" + "slide_type": "slide" }, "tags": [] }, "source": [ - "---\n", + "## Getting help\n", "To get help on a module at the Python shell, import it the whole (the very first way), then you can..." ] },