Skip to content

Commit

Permalink
Edit what not to do section
Browse files Browse the repository at this point in the history
  • Loading branch information
davewhipp committed Sep 24, 2024
1 parent 0294565 commit 54ca295
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 44 deletions.
20 changes: 8 additions & 12 deletions source/part1/chapter-02/md/07-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,23 @@ __future__ cligj linkify_it rvlib
<!-- #endregion -->

<!-- #region editable=true slideshow={"slide_type": ""} -->
## Things to avoid
## Best practices for using modules

Modules are very useful in Python, but there are a few things you should not do.
Here are a few tips for using modules in your Python programs.
<!-- #endregion -->

<!-- #region editable=true slideshow={"slide_type": ""} -->
### from X import *
### Import modules at the start of your files

Do not import functions using `from X import *`. This may be easier to understand by way of an example, but assuming `X` above is a Python module, `from X import *` will import all of the functions in module X. Though you might think this is helpful, it is much better to simply `import X` or `import X as Y` to maintain the connection between the functions and their module. It is also much more likely you will encounter conflicting function names when using `from X import *`.
<!-- #endregion -->
According to the good coding practices described in [PEP 8](https://peps.python.org/pep-0008/#imports) [^pep8], we should always import modules at the top of a file. In this section, we have demonstrated how to import different modules along the way, but in general it is better to import required modules as the very first thing. PEP 8 refers more to traditional script files, but we can apply the guideline to Jupyter Notebook files as well by placing `import` statements in the first code cell of the notebook.

<!-- #region editable=true slideshow={"slide_type": ""} -->
### Poor names when renaming on import
### Avoid importing functions using wildcards

Do not use confusing names when renaming on import. Be smart when you import modules, and follow generally used conventions (`import pandas as pd` is a good way to do things!). If you want to make the module name shorter on import, pick a reasonable abbreviation. For instance, `import matplotlib as m` could be confusing, especially if we used `import math as m` above and might do so in other Jupyter notebooks or script files. Similarly, `import matplotlib as math` is perfectly OK syntax in Python, but bound to cause trouble. Remember, people need to be able to read and understand the code you write. Keep it simple and logical.
<!-- #endregion -->
It is best not to import many functions from a module using the form `from X import *`, where `X` is a Python module. `from X import *` will import all of the functions in module X, and though you might think this is helpful, it is much better to simply `import X` or `import X as Y` to maintain the connection between the functions and their module. In addition to losing the connection between the module and the function, it is much more likely you will encounter conflicting function names when using `from X import *`.

<!-- #region editable=true slideshow={"slide_type": ""} -->
### Importing modules within notebook or script files
### Choose logical names when renaming on import

According to the good coding practices described in [PEP 8](https://peps.python.org/pep-0008/#imports) [^pep8], we should always import modules at the top of a file. In this lesson, we are demonstrating how to import different modules along the way, but in general it would be better to import required modules as the very first thing. PEP 8 refers more to traditional script files, but we can apply the guideline to Jupyter Notebook files by placing our imports the first code cell in the notebook.
Do not use confusing names when renaming on import. Be smart when you import modules, and follow generally used conventions (`import pandas as pd` is a good way to do things!). If you want to make the module name shorter on import, pick a reasonable abbreviation. For instance, `import matplotlib as m` could be confusing, especially if we used `import math as m` above and might do so in other Jupyter notebooks or script files. Similarly, `import matplotlib as math` is perfectly OK syntax in Python, but bound to cause trouble. Remember, people need to be able to read and understand the code you write. Keep it simple and logical.
<!-- #endregion -->

<!-- #region editable=true slideshow={"slide_type": ""} -->
Expand Down
42 changes: 10 additions & 32 deletions source/part1/chapter-02/nb/07-modules.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@
"tags": []
},
"source": [
"## Things to avoid\n",
"## Best practices for using modules\n",
"\n",
"Modules are very useful in Python, but there are a few things you should not do."
"Here are a few tips for using modules in your Python programs."
]
},
{
Expand All @@ -481,39 +481,17 @@
"tags": []
},
"source": [
"### from X import *\n",
"### Import modules at the start of your files\n",
"\n",
"Do not import functions using `from X import *`. This may be easier to understand by way of an example, but assuming `X` above is a Python module, `from X import *` will import all of the functions in module X. Though you might think this is helpful, it is much better to simply `import X` or `import X as Y` to maintain the connection between the functions and their module. It is also much more likely you will encounter conflicting function names when using `from X import *`."
]
},
{
"cell_type": "markdown",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"### Poor names when renaming on import\n",
"According to the good coding practices described in [PEP 8](https://peps.python.org/pep-0008/#imports) [^pep8], we should always import modules at the top of a file. In this section, we have demonstrated how to import different modules along the way, but in general it is better to import required modules as the very first thing. PEP 8 refers more to traditional script files, but we can apply the guideline to Jupyter Notebook files as well by placing `import` statements in the first code cell of the notebook.\n",
"\n",
"Do not use confusing names when renaming on import. Be smart when you import modules, and follow generally used conventions (`import pandas as pd` is a good way to do things!). If you want to make the module name shorter on import, pick a reasonable abbreviation. For instance, `import matplotlib as m` could be confusing, especially if we used `import math as m` above and might do so in other Jupyter notebooks or script files. Similarly, `import matplotlib as math` is perfectly OK syntax in Python, but bound to cause trouble. Remember, people need to be able to read and understand the code you write. Keep it simple and logical."
]
},
{
"cell_type": "markdown",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"### Importing modules within notebook or script files\n",
"### Avoid importing functions using wildcards\n",
"\n",
"According to the good coding practices described in [PEP 8](https://peps.python.org/pep-0008/#imports) [^pep8], we should always import modules at the top of a file. In this lesson, we are demonstrating how to import different modules along the way, but in general it would be better to import required modules as the very first thing. PEP 8 refers more to traditional script files, but we can apply the guideline to Jupyter Notebook files by placing our imports the first code cell in the notebook."
"It is best not to import many functions from a module using the form `from X import *`, where `X` is a Python module. `from X import *` will import all of the functions in module X, and though you might think this is helpful, it is much better to simply `import X` or `import X as Y` to maintain the connection between the functions and their module. In addition to losing the connection between the module and the function, it is much more likely you will encounter conflicting function names when using `from X import *`.\n",
"\n",
"### Choose logical names when renaming on import\n",
"\n",
"Do not use confusing names when renaming on import. Be smart when you import modules, and follow generally used conventions (`import pandas as pd` is a good way to do things!). If you want to make the module name shorter on import, pick a reasonable abbreviation. For instance, `import matplotlib as m` could be confusing, especially if we used `import math as m` above and might do so in other Jupyter notebooks or script files. Similarly, `import matplotlib as math` is perfectly OK syntax in Python, but bound to cause trouble. Remember, people need to be able to read and understand the code you write. Keep it simple and logical."
]
},
{
Expand Down

0 comments on commit 54ca295

Please sign in to comment.