# SOFTWARE ENGINEERING AT BLOCKFUSE LABS : WEEK 17 Week 17 had us looking at two important concepts in python, decorators and error handling in python. **WEEK 17 RECAP TOPICS/COURSES** **DECORATORS:** In Python, decorators are flexible way to modify or extend behaviour of functions or methods, without changing their actual code. This really helps in making our code very reusable and very flexible to maintain. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are often used in scenarios such as logging, authentication and memorization, allowing us to add additional functionality to existing functions or methods in a clean, reusable way. Example: Let's see an example in which a simple Python decorator adds behaviour before and after a function call. ``` def decorator(func): def wrapper(): print("Before calling the function.") func() print("After calling the function.") return wrapper @decorator # Applying the decorator to a function def greet(): print("Hello, World!") greet() ``` **Decorator with Parameters:** Decorators often need to work with functions that have arguments. We use *args and **kwargs so our wrapper can accept any number of arguments. Example: Let's see an example of a decorator that adds functionality before and after a function call. ``` def decorator_name(func): def wrapper(*args, **kwargs): print("Before execution") result = func(*args, **kwargs) print("After execution") return result return wrapper @decorator_name def add(a, b): return a + b print(add(5, 3)) ``` ![Screenshot from 2025-10-05 10-33-08](https://hackmd.io/_uploads/r1LghGeagx.png) **Types of decorators:** 1. Function Decorators The most common decorators in Python, used to wrap and enhance functions by adding extra behaviour before or after the original function runs. 2. Method Decorators Special decorators used for methods inside a class. They work like function decorators but handle the self parameter for instance methods. 3. Class Decorators Class decorators are used to modify or enhance behaviour of a class. Like function decorators, class decorators are applied to class definition. They work by taking class as an argument and returning a modified version of class. **ERROR HANDLING:** **Exceptions** Each time your code tries to do something wrong/foolish/irresponsible/crazy/unenforceable, Python does two things: 1. it stops your program; 2. it creates a special kind of data, called an exception. Both of these activities are called raising an exception. We can say that Python always raises an exception (or that an exception has been raised) when it has no idea what to do with your code. **What happens next?** the raised exception expects somebody or something to notice it and take care of it; if nothing happens to take care of the raised exception, the program will be forcibly terminated, and you will see an error message sent to the console by Python; otherwise, if the exception is taken care of and handled properly, the suspended program can be resumed and its execution can continue. Python provides effective tools that allow you to observe exceptions, identify them and handle them efficiently. This is possible due to the fact that all potential exceptions have their unambiguous names, so you can categorize them and react appropriately. Python handles errors using the try-except approach; 1.The try keyword begins a block of the code which may or may not be performing correctly; 2. Next, Python tries to perform the risky action; if it fails, an exception is raised and Python starts to look for a solution; 3. The except keyword starts a piece of code which will be executed if anything inside the try block goes wrong - if an exception is raised inside a previous try block, it will fail here, so the code located after the except keyword should provide an adequate reaction to the raised exception; returning to the previous nesting level ends the try-except section ![Screenshot from 2025-10-05 11-06-30](https://hackmd.io/_uploads/H1xYhzlalg.png) **Custom error handling:** Python provides a way for us to handle errors that are not by part of the built-in error handling library. Creating a custom exception requires you to inherit from the Exception class. ![Screenshot from 2025-10-05 11-10-18](https://hackmd.io/_uploads/SyoshzxTgg.png) **CONCLUSION** Decorators may look like an ordinary concept but they are really efficient and powerful when it comes to python programming and an in depth look at them will be very beneficial to any developer. Error handling is an essential skill in good developer must have to be able to communicate your code not just to other developers but to users as well.