{"id":204,"date":"2024-10-24T01:57:00","date_gmt":"2024-10-23T20:27:00","guid":{"rendered":"https:\/\/griffso.com\/blogs\/?p=204"},"modified":"2024-10-24T11:09:40","modified_gmt":"2024-10-24T05:39:40","slug":"what-are-python-decorators-and-how-are-they-used","status":"publish","type":"post","link":"https:\/\/griffso.com\/blogs\/what-are-python-decorators-and-how-are-they-used\/","title":{"rendered":"What are Python decorators and how are they used?"},"content":{"rendered":"\n<p><strong><u>DECORATORS<\/u><\/strong><\/p>\n\n\n\n<p>Adding functionality without changing the main function is called Decorators.<\/p>\n\n\n\n<p>A decorators is a function which takes another function as an argument, add some extra functionality and return another function without altering The source code of the original function.<\/p>\n\n\n\n<p>Decorators also called as META Programming<\/p>\n\n\n\n<p><strong><u>reason:<\/u><\/strong>A part of the program Tries to modify another part of the program at compile time.<\/p>\n\n\n\n<p><strong><u>GENERAL STRUCTURE OF DECORATOR<\/u><\/strong><\/p>\n\n\n\n<p>1)A decorators function must be an nested function were in the outer function must take only one argument (Decorator function address)<\/p>\n\n\n\n<p>2)The nested\/inner function must take variable numbers of arguments.<\/p>\n\n\n\n<p>3)The outer function must return the nested\/inner function address.<\/p>\n\n\n\n<p>4)The parameters of the outer function should be called inside the nested function.<\/p>\n\n\n\n<p><strong><u>syntax:<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer (func):\n\ndef inner(*args, **kwargs):\n\nprint(modified main func)\n\nfunc(*args, **kwargs)\n\nreturn inner\n\n@outer\n\ndef main_func():\n\nstatements\n\nreturn statement\n\nprint(main_func(arguments)\\<\/code><\/pre>\n\n\n\n<p><strong><u>Example:<\/u><\/strong><\/p>\n\n\n\n<p>WADF TO ADD 2 NUMBERS AND DISPLAY THE MESSAGE BEFORE AND AFTER PERFORMING THE ADDITION<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def addition(func): #func==add\n\ndef inner(*args):\n\nprint(\"im performing addition\")\n\nfunc(*args)\n\nprint(\"addition is done\")\n\nreturn inner\n\n@addition\n\ndef add(*args):\n\nc=sum(args)\n\nprint(c)\n\nadd(1,2,3,4,5,3,4,6,8)<\/code><\/pre>\n\n\n\n<p><strong>Common Use Cases of Decorators:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Logging:<\/strong>&nbsp;Record function calls, arguments, and return values.<\/li>\n\n\n\n<li><strong>Timing:<\/strong>&nbsp;Measure the execution time of functions.<\/li>\n\n\n\n<li><strong>Caching:<\/strong>&nbsp;Store function results for faster subsequent calls.<\/li>\n\n\n\n<li><strong>Authentication:<\/strong>&nbsp;Check user credentials before allowing function execution.<\/li>\n\n\n\n<li><strong>Error Handling:<\/strong>&nbsp;Provide custom error handling for functions.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">Contributer :<strong> <\/strong>Rohit Singh Rawat<\/pre>\n\n\n\n<p><\/p>\n<div class=\"pld-like-dislike-wrap pld-template-1\">\n    <div class=\"pld-like-wrap  pld-common-wrap\">\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"204\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\n                        <i class=\"fas fa-thumbs-up\"><\/i>\n                <\/a>\n    <span class=\"pld-like-count-wrap pld-count-wrap\">4    <\/span>\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"204\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\n                        <i class=\"fas fa-thumbs-down\"><\/i>\n                <\/a>\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>DECORATORS Adding functionality without changing the main function is called Decorators. A decorators is a function which takes another function as an argument, add some extra functionality and return another function without altering The source code of the original function. Decorators also called as META Programming reason:A part of the program Tries to modify another [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-coding-help-programming-tips-code-examples-coding-resources-debug-guide-software-development-learn-to-code-coding-best-practices-coding-challenges-coding-tutorials"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":2,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":218,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/204\/revisions\/218"}],"wp:attachment":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}