diff --git a/.vscode/markdown-styles.css b/.vscode/markdown-styles.css index fc5deeb..ea90f47 100644 --- a/.vscode/markdown-styles.css +++ b/.vscode/markdown-styles.css @@ -1,6 +1,6 @@ code { - font-family: "IBM Plex Mono"; - font-size: 13px !important; + font-family: "Fira Code"; + font-size: 14px !important; } body { diff --git a/Programming_Languages/Python/Concepts/Python_modules.md b/Programming_Languages/Python/Concepts/Python_modules.md index eab4b53..4e8fb1a 100644 --- a/Programming_Languages/Python/Concepts/Python_modules.md +++ b/Programming_Languages/Python/Concepts/Python_modules.md @@ -6,7 +6,7 @@ tags: [python, OOP] # 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 @@ -49,6 +49,8 @@ def _special_function(): ## 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`: ### Method one @@ -86,7 +88,7 @@ from utils import * printer(default_shape) -shape =Shape("circle") +shape = Shape("circle") ``` ### Importing a subset of the module @@ -104,3 +106,32 @@ And you can use an alias, e.g: ```py 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() +``` diff --git a/Programming_Languages/Python/Syntax/Error_handling_in_Python.md b/Programming_Languages/Python/Syntax/Error_handling_in_Python.md new file mode 100644 index 0000000..73b0f19 --- /dev/null +++ b/Programming_Languages/Python/Syntax/Error_handling_in_Python.md @@ -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. + +