{"id":2412,"date":"2023-01-31T19:55:47","date_gmt":"2023-01-31T19:55:47","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2412"},"modified":"2023-01-31T19:57:47","modified_gmt":"2023-01-31T19:57:47","slug":"python-decorators","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/01\/31\/python-decorators\/","title":{"rendered":"Python: Decorators"},"content":{"rendered":"\n<p>Let&#8217;s learn about decorators <code>@iamdecorator<\/code> <\/p>\n\n\n\n<p>Decorators are used to modify or extend the behavior of functions and classes. They are useful for wrapping a function or class with additional functionality, such as logging, access control, performance measurement, or adding attributes. This can reduce code duplication, increase readability and maintainability.<\/p>\n\n\n\n<p>Decorators in Python are a way to modify or extend the behavior of a function, class, or method without changing their source code.<\/p>\n\n\n\n<p>They are implemented as special types of functions that can take another function or class as an argument, add or change its behavior, and then return the modified function or class<\/p>\n\n\n\n<p>Decorators are indicated with the &#8220;@&#8221; symbol in front of the function or class definition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background\"><code>def logging_decorator(func):\n    def wrapper(*args, **kwargs):\n        print(f\"Running {func.__name__} with args: {args}, {kwargs}\")\n        result = func(*args, **kwargs)\n        print(f\"{func.__name__} returned: {result}\")\n        return result\n    return wrapper\n\n@logging_decorator\ndef add(a, b):\n    return a + b\n\nprint(add(3, 4))<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-vivid-cyan-blue-background-color has-text-color has-background\"><code>Running add with args: (3, 4), {}\nadd returned: 7\n7<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s learn about decorators @iamdecorator Decorators are used to modify or extend the behavior of functions and classes. They are useful for wrapping a function or class with additional functionality, such as logging, access control, performance measurement, or adding attributes. This can reduce code duplication, increase readability and maintainability. Decorators in Python are a way [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[273,270],"class_list":["post-2412","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-decorators","tag-python","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2412"}],"version-history":[{"count":2,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2412\/revisions"}],"predecessor-version":[{"id":2414,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2412\/revisions\/2414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}