|
4470 | 4470 | }, |
4471 | 4471 | { |
4472 | 4472 | "cell_type": "markdown", |
4473 | | - "id": "9e1c43a8", |
| 4473 | + "id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e", |
4474 | 4474 | "metadata": { |
4475 | 4475 | "editable": false, |
4476 | 4476 | "slideshow": { |
4477 | | - "slide_type": "slide" |
| 4477 | + "slide_type": "" |
4478 | 4478 | }, |
4479 | 4479 | "tags": [] |
4480 | 4480 | }, |
4481 | 4481 | "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" |
4483 | 4486 | ] |
4484 | 4487 | }, |
4485 | 4488 | { |
4486 | 4489 | "cell_type": "markdown", |
4487 | | - "id": "eed64626", |
| 4490 | + "id": "9e1c43a8", |
4488 | 4491 | "metadata": { |
4489 | 4492 | "editable": false, |
4490 | 4493 | "slideshow": { |
4491 | | - "slide_type": "" |
4492 | | - } |
| 4494 | + "slide_type": "slide" |
| 4495 | + }, |
| 4496 | + "tags": [] |
4493 | 4497 | }, |
4494 | 4498 | "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_" |
4496 | 4500 | ] |
4497 | 4501 | }, |
4498 | 4502 | { |
|
4502 | 4506 | "editable": false, |
4503 | 4507 | "slideshow": { |
4504 | 4508 | "slide_type": "" |
4505 | | - } |
| 4509 | + }, |
| 4510 | + "tags": [] |
4506 | 4511 | }, |
4507 | 4512 | "source": [ |
4508 | 4513 | "There are several ways of importing _modules_:" |
|
4601 | 4606 | "print(square_root(25))" |
4602 | 4607 | ] |
4603 | 4608 | }, |
| 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 | + }, |
4604 | 4674 | { |
4605 | 4675 | "cell_type": "code", |
4606 | 4676 | "execution_count": null, |
|
4614 | 4684 | }, |
4615 | 4685 | "outputs": [], |
4616 | 4686 | "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", |
4619 | 4688 | "from math import *\n", |
4620 | | - "print(int(sqrt(4.0)))" |
| 4689 | + "print(sqrt(4.0))" |
4621 | 4690 | ] |
4622 | 4691 | }, |
4623 | 4692 | { |
|
4626 | 4695 | "metadata": { |
4627 | 4696 | "editable": false, |
4628 | 4697 | "slideshow": { |
4629 | | - "slide_type": "fragment" |
| 4698 | + "slide_type": "slide" |
4630 | 4699 | }, |
4631 | 4700 | "tags": [] |
4632 | 4701 | }, |
4633 | 4702 | "source": [ |
4634 | | - "---\n", |
| 4703 | + "## Getting help\n", |
4635 | 4704 | "To get help on a module at the Python shell, import it the whole (the very first way), then you can..." |
4636 | 4705 | ] |
4637 | 4706 | }, |
|
0 commit comments