{"id":115,"date":"2026-03-02T08:50:28","date_gmt":"2026-03-02T03:50:28","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=115"},"modified":"2026-03-16T09:52:39","modified_gmt":"2026-03-16T04:52:39","slug":"decorators","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/decorators\/","title":{"rendered":"Decorators"},"content":{"rendered":"\n<p>Decorators are a powerful feature in Python that allow you to modify or extend the behavior of a function or method without changing its actual code.<\/p>\n\n\n\n<p>In simple words:<br>A decorator adds extra functionality to an existing function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Use Decorators?<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add extra behavior (like logging, validation, authentication)<\/li>\n\n\n\n<li>Avoid code repetition<\/li>\n\n\n\n<li>Keep code clean and reusable<\/li>\n\n\n\n<li>Follow DRY principle (Don&#8217;t Repeat Yourself)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Concept<\/strong><\/h2>\n\n\n\n<p>In Python, functions are first-class objects.<br>This means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions can be stored in variables<\/li>\n\n\n\n<li>Functions can be passed as arguments<\/li>\n\n\n\n<li>Functions can return other functions<\/li>\n<\/ul>\n\n\n\n<p>Decorators use this concept.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Example Without @ Syntax<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet():<br>    print(\"Hello\")def decorator_function(func):<br>    def wrapper():<br>        print(\"Before function call\")<br>        func()<br>        print(\"After function call\")<br>    return wrappergreet = decorator_function(greet)<br>greet()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Before function call<br>Hello<br>After function call<\/pre>\n\n\n\n<p>The decorator added extra behavior before and after the function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using @ Decorator Syntax<\/strong><\/h2>\n\n\n\n<p>Python provides a cleaner way using <code>@<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def decorator_function(func):<br>    def wrapper():<br>        print(\"Before function call\")<br>        func()<br>        print(\"After function call\")<br>    return wrapper@decorator_function<br>def greet():<br>    print(\"Hello\")greet()<\/pre>\n\n\n\n<p>This is the same as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">greet = decorator_function(greet)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Decorator with Arguments<\/strong><\/h2>\n\n\n\n<p>If the original function has parameters:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def decorator_function(func):<br>    def wrapper(name):<br>        print(\"Before function call\")<br>        func(name)<br>        print(\"After function call\")<br>    return wrapper@decorator_function<br>def greet(name):<br>    print(\"Hello\", name)greet(\"Hira\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">**Using *args and <strong>kwargs (Best Practice)<\/strong><\/h2>\n\n\n\n<p>To handle any number of arguments:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def decorator_function(func):<br>    def wrapper(*args, **kwargs):<br>        print(\"Before function call\")<br>        result = func(*args, **kwargs)<br>        print(\"After function call\")<br>        return result<br>    return wrapper<\/pre>\n\n\n\n<p>This makes the decorator flexible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Built-in Decorators in Python<\/strong><\/h2>\n\n\n\n<p><code>@staticmethod<\/code><br><code>@classmethod<\/code><br><code>@property<\/code><\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, marks):<br>        self._marks = marks    @property<br>    def marks(self):<br>        return self._marks<\/pre>\n\n\n\n<p>Now <code>marks<\/code> can be accessed like a variable, not a method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Points<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Decorators take a function as input<\/li>\n\n\n\n<li>They return a new function<\/li>\n\n\n\n<li>Use <code>@<\/code> syntax for cleaner code<\/li>\n\n\n\n<li>Useful for logging, timing, authentication, validation<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaway<\/strong><\/h2>\n\n\n\n<p>Decorators allow you to add extra functionality to functions or methods without modifying their original code.<\/p>\n\n\n\n<p>They make programs cleaner, reusable, and more powerful in Python.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Decorators<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773636870550\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":57,"template":"","class_list":["post-115","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Decorators - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python decorators, how they modify functions, create reusable wrappers, and improve code structure with practical examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/decorators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Decorators - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python decorators, how they modify functions, create reusable wrappers, and improve code structure with practical examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/decorators\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T04:52:39+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/decorators\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/decorators\\\/\",\"name\":\"Decorators - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:50:28+00:00\",\"dateModified\":\"2026-03-16T04:52:39+00:00\",\"description\":\"Learn Python decorators, how they modify functions, create reusable wrappers, and improve code structure with practical examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/decorators\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/decorators\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/decorators\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Decorators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Decorators - One Language. Endless Possibilities","description":"Learn Python decorators, how they modify functions, create reusable wrappers, and improve code structure with practical examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/decorators\/","og_locale":"en_US","og_type":"article","og_title":"Decorators - One Language. Endless Possibilities","og_description":"Learn Python decorators, how they modify functions, create reusable wrappers, and improve code structure with practical examples.","og_url":"https:\/\/gigz.pk\/python\/lesson\/decorators\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-16T04:52:39+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/decorators\/","url":"https:\/\/gigz.pk\/python\/lesson\/decorators\/","name":"Decorators - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:50:28+00:00","dateModified":"2026-03-16T04:52:39+00:00","description":"Learn Python decorators, how they modify functions, create reusable wrappers, and improve code structure with practical examples.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/decorators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/decorators\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/decorators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Decorators"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}