Skip to content

Commit d3e1484

Browse files
committed
update module section
1 parent 831c105 commit d3e1484

File tree

2 files changed

+185
-62
lines changed

2 files changed

+185
-62
lines changed

Intermediate.ipynb

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,29 +4470,33 @@
44704470
},
44714471
{
44724472
"cell_type": "markdown",
4473-
"id": "9e1c43a8",
4473+
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
44744474
"metadata": {
44754475
"editable": false,
44764476
"slideshow": {
4477-
"slide_type": "slide"
4477+
"slide_type": ""
44784478
},
44794479
"tags": []
44804480
},
44814481
"source": [
4482-
"## Importing _modules_"
4482+
"Modules let us:\n",
4483+
"- Use code that others have already written and tested\n",
4484+
"- Load in only the functionality we need\n",
4485+
"- Know exactly where a piece of functionality comes from"
44834486
]
44844487
},
44854488
{
44864489
"cell_type": "markdown",
4487-
"id": "eed64626",
4490+
"id": "9e1c43a8",
44884491
"metadata": {
44894492
"editable": false,
44904493
"slideshow": {
4491-
"slide_type": ""
4492-
}
4494+
"slide_type": "slide"
4495+
},
4496+
"tags": []
44934497
},
44944498
"source": [
4495-
"Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet."
4499+
"## Importing _modules_"
44964500
]
44974501
},
44984502
{
@@ -4502,7 +4506,8 @@
45024506
"editable": false,
45034507
"slideshow": {
45044508
"slide_type": ""
4505-
}
4509+
},
4510+
"tags": []
45064511
},
45074512
"source": [
45084513
"There are several ways of importing _modules_:"
@@ -4601,6 +4606,71 @@
46014606
"print(square_root(25))"
46024607
]
46034608
},
4609+
{
4610+
"cell_type": "markdown",
4611+
"id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275",
4612+
"metadata": {
4613+
"editable": false,
4614+
"slideshow": {
4615+
"slide_type": "slide"
4616+
},
4617+
"tags": []
4618+
},
4619+
"source": [
4620+
"## Why Namespaces Matter\n",
4621+
"\n",
4622+
"The word `log` means two very different things:"
4623+
]
4624+
},
4625+
{
4626+
"cell_type": "code",
4627+
"execution_count": null,
4628+
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
4629+
"metadata": {
4630+
"editable": true,
4631+
"slideshow": {
4632+
"slide_type": ""
4633+
},
4634+
"tags": []
4635+
},
4636+
"outputs": [],
4637+
"source": [
4638+
"import logging\n",
4639+
"\n",
4640+
"math.log(100, 10) # A logarithm: 2.0\n",
4641+
"\n",
4642+
"logging.log(20, \"Hello\") # A logbook entry: records a message"
4643+
]
4644+
},
4645+
{
4646+
"cell_type": "markdown",
4647+
"id": "36f46ead-b9a1-481f-8798-edbfc58b5504",
4648+
"metadata": {
4649+
"editable": false,
4650+
"slideshow": {
4651+
"slide_type": ""
4652+
},
4653+
"tags": []
4654+
},
4655+
"source": [
4656+
"Without namespaces, Python, and your reader, couldn't tell them apart."
4657+
]
4658+
},
4659+
{
4660+
"cell_type": "markdown",
4661+
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
4662+
"metadata": {
4663+
"editable": false,
4664+
"slideshow": {
4665+
"slide_type": "fragment"
4666+
},
4667+
"tags": []
4668+
},
4669+
"source": [
4670+
"----\n",
4671+
"This is why importing a whole module into namespace is usually a bad idea:"
4672+
]
4673+
},
46044674
{
46054675
"cell_type": "code",
46064676
"execution_count": null,
@@ -4614,10 +4684,9 @@
46144684
},
46154685
"outputs": [],
46164686
"source": [
4617-
"# import all functions, variables, and classes from a module into the global namespace\n",
4618-
"# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n",
4687+
"# import all functions, variables, and classes from a module - better to avoid this\n",
46194688
"from math import *\n",
4620-
"print(int(sqrt(4.0)))"
4689+
"print(sqrt(4.0))"
46214690
]
46224691
},
46234692
{
@@ -4626,12 +4695,12 @@
46264695
"metadata": {
46274696
"editable": false,
46284697
"slideshow": {
4629-
"slide_type": "fragment"
4698+
"slide_type": "slide"
46304699
},
46314700
"tags": []
46324701
},
46334702
"source": [
4634-
"---\n",
4703+
"## Getting help\n",
46354704
"To get help on a module at the Python shell, import it the whole (the very first way), then you can..."
46364705
]
46374706
},

0 commit comments

Comments
 (0)