Autosave: 2023-03-06 07:41:40

This commit is contained in:
thomasabishop 2023-03-06 07:41:40 +00:00
parent 6667de67df
commit cb53c5e805
4 changed files with 71 additions and 4 deletions

View file

@ -1,6 +1,6 @@
code { code {
font-family: "IBM Plex Mono"; font-family: "Fira Code";
font-size: 13px !important; font-size: 14px !important;
} }
body { body {

View file

@ -6,7 +6,7 @@ tags: [python, OOP]
# Python modules # Python modules
Modules in Python are very similar to modules in Node.js: a mix of several pieces of functionality (often written in a mix of syntactic styles) bundled together to be used as a unit. Modules in Python are very similar to modules in Node.js: a combination of several pieces of functionality (often written in a mix of different syntactic styles) bundled together to be used as a unit.
## Creating modules ## Creating modules
@ -49,6 +49,8 @@ def _special_function():
## Importing modules ## Importing modules
> Whether we are importing our own local module, a third-party module or a built-in module native to Python, the syntax is the same.
To use the module defined in `utils.py`: To use the module defined in `utils.py`:
### Method one ### Method one
@ -86,7 +88,7 @@ from utils import *
printer(default_shape) printer(default_shape)
shape =Shape("circle") shape = Shape("circle")
``` ```
### Importing a subset of the module ### Importing a subset of the module
@ -104,3 +106,32 @@ And you can use an alias, e.g:
```py ```py
from util import Shape as banana from util import Shape as banana
``` ```
## Main module as program entry point
If we have any freestanding code in a module. It will execute the moment the file it is imported into executes. Examples of this in the module example are the print function at the top and the instantiation of the `shape` object.
Typically you will only want this to happen when **the module is the entrypoint** to the program. In Python the entrypoint is known as the `main` module. This is analagous to `index.js` in a Node program.
When the module is the entry point it has a special name: `__main__`. We can test for this condition and only run code if the module is being run as the main module. This is a common pattern:
```py
if __name__ == "__main__":
print("This runs only if the module is the main module")
```
`__name__` is a reference to the current module name.
> We do not need to name our module `main` for this to occur. Whenever an imported module is run it occupies the position of `__main__`
To ensure that certain code runs on initialisation we can define a special `main` function or class method that runs when the module is loaded. This is effectively the set-up code in the entry point, e.g:
```py
def main():
printer(default_shape)
shape = Shape("circle")
printer(shape)
if __name__ == "__main__":
main()
```

View file

@ -0,0 +1,36 @@
---
categories:
- Programming Languages
tags: [python]
---
# Error handling and exceptions in Python
Errors can typically occur in the following scenarios:
- A file or resource that is referred to does not exist
- A network connection fails
- A module import error occurs
Therefore in these operations you would want to define explicit error handling contingencies, so that you can detect errors and respond appropriately.
## Difference between errors and exceptions.
Errors and exceptions are both types of runtime problems that can occur in the program however they slightly different from each other.
In essence, errors are problems in the program's code that prevent it from running. Exceptions are raised by the program itself when it encounters a problem at runtime that it can't handle.
<dl>
<dt><b>Errors</b></dt>
<dd> are problems that occur at runtime that prevent the program from executing successfully. Errors are usually caused by issues in the program's code, such as syntax errors or logical errors. Examples of errors in Python include NameError, SyntaxError, and TypeError.</dd>
<dt><b>Exceptions</b></dt>
<dd>are raised by the program itself when it encounters an unexpected situation at runtime that it cannot handle. Exceptions allow the program to gracefully handle errors and continue running without crashing. Examples of exceptions in Python include ZeroDivisionError, FileNotFoundError, and TypeError.</dd>
</dl>
## The Exception hierarchy
Errors and Exceptions are objects in Python and there is no real syntactic distinction between the two since all errors and exceptions in herit from a base exception class.
The root class is `BaseException` which all errors and exeptions extend as subclasses:
![](/_img/python-exception-hierarchy.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB