Skip to content

Commit 7cd7188

Browse files
authored
Merge pull request DurhamARC-Training#68 from DurhamARC-Training/62-add-one-slide-why-modules
62 add one slide why modules
2 parents 8ad6a30 + 4aceb22 commit 7cd7188

File tree

2 files changed

+180
-26
lines changed

2 files changed

+180
-26
lines changed

Intermediate.ipynb

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3961,29 +3961,33 @@
39613961
},
39623962
{
39633963
"cell_type": "markdown",
3964-
"id": "9e1c43a8",
3964+
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
39653965
"metadata": {
3966+
"editable": false,
39663967
"slideshow": {
3967-
"slide_type": "slide"
3968+
"slide_type": ""
39683969
},
3969-
"tags": [],
3970-
"editable": false
3970+
"tags": []
39713971
},
39723972
"source": [
3973-
"## Importing _modules_"
3973+
"Modules let us:\n",
3974+
"- Use code that others have already written and tested\n",
3975+
"- Load in only the functionality we need\n",
3976+
"- Know exactly where a piece of functionality comes from"
39743977
]
39753978
},
39763979
{
39773980
"cell_type": "markdown",
3978-
"id": "eed64626",
3981+
"id": "9e1c43a8",
39793982
"metadata": {
39803983
"slideshow": {
3981-
"slide_type": ""
3984+
"slide_type": "slide"
39823985
},
3986+
"tags": [],
39833987
"editable": false
39843988
},
39853989
"source": [
3986-
"Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet."
3990+
"## Importing _modules_"
39873991
]
39883992
},
39893993
{
@@ -3993,6 +3997,7 @@
39933997
"slideshow": {
39943998
"slide_type": ""
39953999
},
4000+
"tags": [],
39964001
"editable": false
39974002
},
39984003
"source": [
@@ -4087,6 +4092,71 @@
40874092
"print(square_root(25))"
40884093
]
40894094
},
4095+
{
4096+
"cell_type": "markdown",
4097+
"id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275",
4098+
"metadata": {
4099+
"editable": false,
4100+
"slideshow": {
4101+
"slide_type": "slide"
4102+
},
4103+
"tags": []
4104+
},
4105+
"source": [
4106+
"## Why Namespaces Matter\n",
4107+
"\n",
4108+
"The word `log` means two very different things:"
4109+
]
4110+
},
4111+
{
4112+
"cell_type": "code",
4113+
"execution_count": null,
4114+
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
4115+
"metadata": {
4116+
"editable": true,
4117+
"slideshow": {
4118+
"slide_type": ""
4119+
},
4120+
"tags": []
4121+
},
4122+
"outputs": [],
4123+
"source": [
4124+
"import logging\n",
4125+
"\n",
4126+
"math.log(100, 10) # A logarithm: 2.0\n",
4127+
"\n",
4128+
"logging.log(20, \"Hello\") # A logbook entry: records a message"
4129+
]
4130+
},
4131+
{
4132+
"cell_type": "markdown",
4133+
"id": "36f46ead-b9a1-481f-8798-edbfc58b5504",
4134+
"metadata": {
4135+
"editable": false,
4136+
"slideshow": {
4137+
"slide_type": ""
4138+
},
4139+
"tags": []
4140+
},
4141+
"source": [
4142+
"Without namespaces, Python, and your reader, couldn't tell them apart."
4143+
]
4144+
},
4145+
{
4146+
"cell_type": "markdown",
4147+
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
4148+
"metadata": {
4149+
"editable": false,
4150+
"slideshow": {
4151+
"slide_type": "fragment"
4152+
},
4153+
"tags": []
4154+
},
4155+
"source": [
4156+
"----\n",
4157+
"This is why importing a whole module into namespace is usually a bad idea:"
4158+
]
4159+
},
40904160
{
40914161
"cell_type": "code",
40924162
"execution_count": null,
@@ -4099,24 +4169,23 @@
40994169
},
41004170
"outputs": [],
41014171
"source": [
4102-
"# import all functions, variables, and classes from a module into the global namespace\n",
4103-
"# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n",
4172+
"# import all functions, variables, and classes from a module - better to avoid this\n",
41044173
"from math import *\n",
4105-
"print(int(sqrt(4.0)))"
4174+
"print(sqrt(4.0))"
41064175
]
41074176
},
41084177
{
41094178
"cell_type": "markdown",
41104179
"id": "d1cc0855",
41114180
"metadata": {
41124181
"slideshow": {
4113-
"slide_type": "fragment"
4182+
"slide_type": "slide"
41144183
},
41154184
"tags": [],
41164185
"editable": false
41174186
},
41184187
"source": [
4119-
"---\n",
4188+
"## Getting help\n",
41204189
"To get help on a module at the Python shell, import it the whole (the very first way), then you can..."
41214190
]
41224191
},

Intermediate_full.ipynb

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,27 +4077,32 @@
40774077
},
40784078
{
40794079
"cell_type": "markdown",
4080-
"id": "9e1c43a8",
4080+
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
40814081
"metadata": {
4082+
"editable": true,
40824083
"slideshow": {
4083-
"slide_type": "slide"
4084+
"slide_type": ""
40844085
},
40854086
"tags": []
40864087
},
40874088
"source": [
4088-
"## Importing _modules_"
4089+
"Modules let us:\n",
4090+
"- Use code that others have already written and tested\n",
4091+
"- Load in only the functionality we need\n",
4092+
"- Know exactly where a piece of functionality comes from"
40894093
]
40904094
},
40914095
{
40924096
"cell_type": "markdown",
4093-
"id": "eed64626",
4097+
"id": "9e1c43a8",
40944098
"metadata": {
40954099
"slideshow": {
4096-
"slide_type": ""
4097-
}
4100+
"slide_type": "slide"
4101+
},
4102+
"tags": []
40984103
},
40994104
"source": [
4100-
"Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet."
4105+
"## Importing _modules_"
41014106
]
41024107
},
41034108
{
@@ -4106,7 +4111,8 @@
41064111
"metadata": {
41074112
"slideshow": {
41084113
"slide_type": ""
4109-
}
4114+
},
4115+
"tags": []
41104116
},
41114117
"source": [
41124118
"There are several ways of importing _modules_:"
@@ -4200,6 +4206,86 @@
42004206
"print(square_root(25))"
42014207
]
42024208
},
4209+
{
4210+
"cell_type": "markdown",
4211+
"id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275",
4212+
"metadata": {
4213+
"editable": true,
4214+
"slideshow": {
4215+
"slide_type": "slide"
4216+
},
4217+
"tags": []
4218+
},
4219+
"source": [
4220+
"## Why Namespaces Matter\n",
4221+
"\n",
4222+
"The word `log` means two very different things:"
4223+
]
4224+
},
4225+
{
4226+
"cell_type": "code",
4227+
"execution_count": null,
4228+
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
4229+
"metadata": {
4230+
"editable": true,
4231+
"slideshow": {
4232+
"slide_type": ""
4233+
},
4234+
"tags": []
4235+
},
4236+
"outputs": [],
4237+
"source": [
4238+
"import logging\n",
4239+
"\n",
4240+
"math.log(100, 10) # A logarithm: 2.0\n",
4241+
"\n",
4242+
"logging.log(20, \"Hello\") # A logbook entry: records a message"
4243+
]
4244+
},
4245+
{
4246+
"cell_type": "markdown",
4247+
"id": "36f46ead-b9a1-481f-8798-edbfc58b5504",
4248+
"metadata": {
4249+
"editable": true,
4250+
"slideshow": {
4251+
"slide_type": ""
4252+
},
4253+
"tags": []
4254+
},
4255+
"source": [
4256+
"Without namespaces, Python, and your reader, couldn't tell them apart."
4257+
]
4258+
},
4259+
{
4260+
"cell_type": "markdown",
4261+
"id": "ccbef34a-9226-4735-859d-378640ee9cc7",
4262+
"metadata": {
4263+
"editable": true,
4264+
"slideshow": {
4265+
"slide_type": "notes"
4266+
},
4267+
"tags": []
4268+
},
4269+
"source": [
4270+
"<ins>Notes:</ins>\n",
4271+
"- In JupyterLite, logging output is suppressed by default unless you explicitly configure a handler."
4272+
]
4273+
},
4274+
{
4275+
"cell_type": "markdown",
4276+
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
4277+
"metadata": {
4278+
"editable": true,
4279+
"slideshow": {
4280+
"slide_type": "fragment"
4281+
},
4282+
"tags": []
4283+
},
4284+
"source": [
4285+
"----\n",
4286+
"This is why importing a whole module into namespace is usually a bad idea:"
4287+
]
4288+
},
42034289
{
42044290
"cell_type": "code",
42054291
"execution_count": null,
@@ -4212,23 +4298,22 @@
42124298
},
42134299
"outputs": [],
42144300
"source": [
4215-
"# import all functions, variables, and classes from a module into the global namespace\n",
4216-
"# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n",
4301+
"# import all functions, variables, and classes from a module - better to avoid this\n",
42174302
"from math import *\n",
4218-
"print(int(sqrt(4.0)))"
4303+
"print(sqrt(4.0))"
42194304
]
42204305
},
42214306
{
42224307
"cell_type": "markdown",
42234308
"id": "d1cc0855",
42244309
"metadata": {
42254310
"slideshow": {
4226-
"slide_type": "fragment"
4311+
"slide_type": "slide"
42274312
},
42284313
"tags": []
42294314
},
42304315
"source": [
4231-
"---\n",
4316+
"## Getting help\n",
42324317
"To get help on a module at the Python shell, import it the whole (the very first way), then you can..."
42334318
]
42344319
},

0 commit comments

Comments
 (0)