{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyPFJTTvjD+8cdUJGQWvnXZT", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Mastering Python Decorators\n", "\n", "## Enhance your functions without changing their code\n", "\n", "| ![space-1.jpg](https://github.com/Tanu-N-Prabhu/Python/blob/master/Img/decoraters.png?raw=true) |\n", "|:--:|\n", "| Image Generated Using Canva|\n", "\n", "#### Why It’s So Important\n", "Decorators in Python are one of the most powerful and useful tools, yet they are often underutilized. They allow you to \"wrap\" a function to extend its behavior without permanently modifying it. This becomes extremely handy when adding functionalities like logging, authentication, caching, or performance tracking. Mastering decorators boosts your ability to write clean, modular, and professional-grade code.\n", "\n", "---\n", "\n", "#### Key Highlights\n", "* Add functionality to existing code easily.\n", "* Keeps functions clean and readable.\n", "* Reusable and customizable behavior extensions.\n", "* Essential for building scalable applications.\n", "\n", "---\n", "\n", "#### What is a Decorator?\n", "A decorator is simply a function that takes another function as an argument, adds some functionality, and returns another function.\n", "\n", "##### Example\n", "\n", "\n" ], "metadata": { "id": "-KE5_qxI0rRu" } }, { "cell_type": "code", "source": [ "# Basic decorator that logs the execution of a function\n", "def log_decorator(func):\n", " def wrapper(*args, **kwargs):\n", " print(f\"Function '{func.__name__}' is being called\")\n", " result = func(*args, **kwargs)\n", " print(f\"Function '{func.__name__}' executed successfully\")\n", " return result\n", " return wrapper\n", "\n", "# Applying the decorator\n", "@log_decorator\n", "def greet(name):\n", " print(f\"Hello, {name}!\")\n", "\n", "# Function call\n", "greet(\"Tanu\")\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZeWDeRFR1X8O", "outputId": "5485115d-f9b6-4827-ae0b-29dd235fcec3" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Function 'greet' is being called\n", "Hello, Tanu!\n", "Function 'greet' executed successfully\n" ] } ] }, { "cell_type": "markdown", "source": [ "#### What this shows\n", "* `@log_decorator` automatically wraps the greet function.\n", "* When you call `greet(\"Tanu\")`, the wrapper runs first, logs the messages, then executes the actual function.\n", "\n", "---\n", "\n", "#### Fun Fact\n", "In Python, even built-in features like `@staticmethod` and `@classmethod` are implemented as decorators!\n", "\n", "---\n", "\n", "#### Conclusion\n", "Mastering decorators empowers you to write cleaner, more efficient Python code. By adding functionality without touching the core logic, decorators make your programs more modular, readable, and professional. Thanks for reading my article, let me know if you have any suggestions or similar implementations via the comment section. Until then, see you next time. Happy coding!\n", "\n", "---\n", "\n", "### Before you go\n", "* Be sure to **Like** and **Connect** Me\n", "* Follow Me : [Medium](https://medium.com/@tanunprabhu95) | [GitHub](https://github.com/Tanu-N-Prabhu) | [LinkedIn](https://ca.linkedin.com/in/tanu-nanda-prabhu-a15a091b5) | [Python Hub](https://github.com/Tanu-N-Prabhu/Python)\n", "* [Check out my latest articles on Programming](https://medium.com/@tanunprabhu95)\n", "* Check out my [GitHub](https://github.com/Tanu-N-Prabhu) for code and [Medium](https://medium.com/@tanunprabhu95) for deep dives!\n" ], "metadata": { "id": "JODXLzbK1ael" } } ] }