Skip to content

Commit 8da1720

Browse files
committed
Tweaks to OOP
1 parent 1bd96f7 commit 8da1720

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

05_oop.qmd

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,21 @@ class DataArray:
129129
...
130130
```
131131

132+
. . .
133+
134+
`DataArray` *has a* `geometry` (e.g. `Grid`) and an `item` (`ItemInfo`).
135+
132136
## Inheritance
133137

138+
::: {.incremental}
139+
140+
* Inheritance is a way to reuse code and specialize behavior.
141+
* A child class inherits the attributes and methods from the parent class.
142+
* A child class can override the methods of the parent class.
143+
* A child class can add new methods.
144+
145+
:::
146+
134147
## Inheritance - Example
135148

136149
```{mermaid}
@@ -166,6 +179,11 @@ class GeometryFMVerticalProfile{
166179
_GeometryFMLayered <|-- GeometryFMVerticalProfile
167180
```
168181

182+
. . .
183+
184+
`GeometryFM3D` inherits from `_GeometryFMLayered`, it *is a* `_GeometryFMLayered`.
185+
186+
169187
## Inheritance - Example (2)
170188

171189
```python
@@ -180,6 +198,7 @@ class _GeometryFMLayered(_GeometryFM):
180198
self._n_sigma = n_sigma
181199
```
182200

201+
183202
## Composition vs inheritance
184203

185204
::: {.incremental}
@@ -226,14 +245,13 @@ def repeated_string(s, n):
226245

227246
::: {.incremental}
228247
* Python is a dynamically typed language
229-
* Types are not checked at compile time
230-
* Types are checked at runtime
248+
* Types are not checked at compile time by the interpreter
249+
* Types *can* be checked before runtime using a linter (e.g. `mypy`)
250+
* Type hints can be used by VS Code to provide auto-completion
231251
:::
232252

233253
. . .
234254

235-
**Python** with type hints
236-
237255
```python
238256
n: int = 2
239257
s: str = "Hello"
@@ -379,6 +397,9 @@ Dundermethods:
379397

380398
```python
381399
class JavaLikeToolbox:
400+
401+
def __init__(self, tools: Collection[Tool]):
402+
self.tools = tools
382403

383404
def getToolByName(self, name: str) -> Tool:
384405
for tool in self.tools:
@@ -401,6 +422,9 @@ Hammer()
401422
```python
402423
class Toolbox:
403424

425+
def __init__(self, tools: Collection[Tool]):
426+
self._tools = {tool.name: tool for tool in tools}
427+
404428
def __getitem__(self, name: str) -> Tool:
405429
return self._tools[name]
406430

0 commit comments

Comments
 (0)