Skip to content

Commit

Permalink
shortened import notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Jul 2, 2024
1 parent fe302c6 commit 956a7ab
Showing 1 changed file with 10 additions and 88 deletions.
98 changes: 10 additions & 88 deletions docs/02_python_basics/10_Import_packages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -236,54 +236,7 @@
"id": "23644840-840f-4ecd-b09a-4fee262678ce",
"metadata": {},
"source": [
"Of course in this case, unless we find the specfiic line where the function is imported, we don't know that ```cos``` necessarily belongs to Numpy as we could also have defined such a function in the notebook. If we want to import **all** functions from a package we can also use the ```*``` sign:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "5809f8eb-1a81-46d5-aaff-11c9e672ef47",
"metadata": {},
"outputs": [],
"source": [
"from numpy import *"
]
},
{
"cell_type": "markdown",
"id": "a708b3c9-08d6-4dc9-a902-fc996e0ea1c7",
"metadata": {},
"source": [
"Here we have for example access to the sine function withouth having explicitly imported it:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "79ddc559-52ac-4cc2-acaf-19d2ccc3c4a6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0015926529164868282"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sin(3.14)"
]
},
{
"cell_type": "markdown",
"id": "7461b9f0-c58e-4b31-8cc4-cf0dd15069c6",
"metadata": {},
"source": [
"As you can see, with this solution we have *no idea* what we actually imported in our notebook, which can lead to unwanted effects like re-using a function name. **If you don't have a specific reason to use this import variant, we strongly discourage its use.**"
"Of course in this case, unless we find the specfiic line where the function is imported, we don't know that ```cos``` necessarily belongs to Numpy as we could also have defined such a function in the notebook."
]
},
{
Expand Down Expand Up @@ -397,45 +350,6 @@
"normal()"
]
},
{
"cell_type": "markdown",
"id": "af135b70-e8a0-4d33-8f05-885ddc6855b5",
"metadata": {},
"source": [
"Finally we can rename using the ```as``` statement:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "ef43be12-dc16-4b52-a009-72bf4d798c8e",
"metadata": {},
"outputs": [],
"source": [
"from numpy import random as rd"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "7988aa15-b136-4f58-a553-29ccfa9a9634",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.6781586087709578"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rd.normal()"
]
},
{
"cell_type": "markdown",
"id": "cc770a20-0014-4b6c-b12e-3fa6b1d72f22",
Expand All @@ -448,6 +362,14 @@
"- import just the ```norm``` function and call it\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72dc7b73-b072-411d-92f3-a7c9cce4395f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -466,7 +388,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 956a7ab

Please sign in to comment.