{"id":5799,"date":"2019-09-14T13:06:45","date_gmt":"2019-09-14T07:36:45","guid":{"rendered":"https:\/\/www.csestack.org\/?p=5799"},"modified":"2022-04-11T15:54:18","modified_gmt":"2022-04-11T10:24:18","slug":"python-decorators","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/python-decorators\/","title":{"rendered":"[Step-by-step] Python Decorators Explained with Examples for Beginners"},"content":{"rendered":"\n<p>Python decorators are considered as one of the very advanced and very useful concepts.<\/p>\n\n\n\n<p>In this tutorial, I will explain Python decorators with examples. Going through this tutorial, you will learn to implement Python decorators and how it can be very useful in your project.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fd5bac3fb7f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fd5bac3fb7f\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.csestack.org\/python-decorators\/#What_is_Python_Decorators\" >What is Python Decorators?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.csestack.org\/python-decorators\/#Python_Decorators_Explained\" >Python Decorators Explained<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.csestack.org\/python-decorators\/#Python_Decorator_Basic_Example\" >Python Decorator Basic Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.csestack.org\/python-decorators\/#Python_Decorators_Example_with_Two_Arguments\" >Python Decorators Example with Two Arguments<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.csestack.org\/python-decorators\/#When_to_Use_Python_Decorators\" >When to Use Python Decorators?<\/a><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Python_Decorators\"><\/span>What is Python Decorators?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This is one of the very popular <a href=\"https:\/\/www.csestack.org\/python-interview-questions-answers\/\">Python interview questions<\/a> asked in the IT job interviews.<\/p>\n\n\n\n<p>Python decorators is a technique for changing the behavior of an existing function without changing actual code inside the function.<\/p>\n\n\n\n<p>Even though we are changing the behavior of the function, there should not have any change in the function definition and function call.<\/p>\n\n\n\n<p>It means&#8230;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The code inside the original function should not be changed.<\/li><li>There should not have any change in the function call.<\/li><\/ul>\n\n\n\n<p>Python has a special mechanism called Python decorators to change the behavior of the existing function.<\/p>\n\n\n\n<p>It might be a bit confusing to you. Let&#8217;s go with an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_Decorators_Explained\"><\/span>Python Decorators Explained<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>How does Python Decorator work?<\/p>\n\n\n\n<p>Let&#8217;s start with a simple Python function code.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def myFunc():\n  print(&quot;Hello, World!&quot;)\n\nmyFunc()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Hello, World!<\/pre>\n\n\n\n<p>This is the basic Python program to implement a Python function. If you are new to Python programming, check&nbsp;<a href=\"https:\/\/www.csestack.org\/basic-python3-syntax-code-example\/\">basic Python code syntax<\/a>.<\/p>\n\n\n\n<p>Now consider a function that you want to wrap inside another function (called decorator).<\/p>\n\n\n\n<p>It is similar to the technique of wrapping gifts inside flashy decorative papers.<\/p>\n\n\n\n<p>Let\u2019s see how it can be implemented in Python.<\/p>\n\n\n\n<p>We know, that everything in Python is an object including the Python function. It means, we can pass the function object as an argument to another function. By default, the function name works as a function object.<\/p>\n\n\n\n<p>We can write one Python function inside another function.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def myWrapper(func):\n  def myInnerFunc():\n    print(&quot;Inside wrapper.&quot;)\n    func()\n  return myInnerFunc\n \ndef myFunc():\n  print(&quot;Hello, World!&quot;)\n\nc=myWrapper(myFunc)\nc()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Inside wrapper.\nHello, World!\n<\/pre>\n\n\n\n<p>Here we are passing the function (<code>myFunc<\/code>) as a parameter to the wrapper function (<code>myWapper<\/code>).<\/p>\n\n\n\n<p>This is how it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The actual function is passed as a parameter to the wrapper function.<\/li><li><code>myWrapper<\/code>&nbsp;has an inner function which calls the actual function (<code>myFunc<\/code>).<\/li><li>Inside Inner function, you can write any additional operation to perform along with calling the original function (<code>myFunc<\/code>).<\/li><li><code>myWrapper<\/code>&nbsp;returns the inner function object.<\/li><\/ul>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;When you write function name without brackets, it acts as a function object and it does not call function. The function gets called only when there is a function name (object) followed by brackets \u201c()\u201d.<\/p>\n\n\n\n<p>If you compare the above two programs, you have changed the behavior of the original function. You can see the difference in the output.<\/p>\n\n\n\n<p>We have fulfilled the first criteria of not changing code inside the function.<\/p>\n\n\n\n<p>But the function call has changed<\/p>\n\n\n\n<p>from<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">myFunc()<\/pre>\n\n\n\n<p>to<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c=myWrapper(myFunc)\nc()<\/pre>\n\n\n\n<p>This does not fulfill our second criteria.<\/p>\n\n\n\n<p>Here is the use of Python decorators.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_Decorator_Basic_Example\"><\/span>Python Decorator Basic Example<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def myWrapper(func):\n  def myInnerFunc():\n    print(&quot;Inside wrapper.&quot;)\n    func()\n  return myInnerFunc\n \n@myWrapper\ndef myFunc():\n  print(&quot;Hello, World!&quot;)\n\nmyFunc()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Inside wrapper.\nHello, World!<\/pre>\n\n\n\n<p>Python decorator name (<code>@myWrapper<\/code>) is specified above the actual function definition. (2)<\/p>\n\n\n\n<p><strong>How does the Python decorator program execute?<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"474\" height=\"275\" src=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png\" alt=\"Python Decorators Explained\" class=\"wp-image-5806\" srcset=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png 474w, https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained-300x174.png 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>Below are the steps of execution.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The original function is called (1).<\/li><li>There is a function wrapper name specified above the function definition (2). This indicates, that there is a function decorator assigned to the function.<\/li><li>The decorator function gets called. The program controller passes the function object as a parameter to the decorator function (3).<\/li><li>The function inside the decorator function gets executed (4).<\/li><li>The inner function calls the actual function (5).<\/li><li>The original function starts execution (6).<\/li><\/ul>\n\n\n\n<p>This is a simple Python decorator example. You can also pass the arguments to the Python decorators.<\/p>\n\n\n\n<p>Many get confused decorators with the <a href=\"https:\/\/www.csestack.org\/monkey-patching-python-coding-example\/\">monkey patching techniques in Python<\/a>. Two are the different things.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_Decorators_Example_with_Two_Arguments\"><\/span>Python Decorators Example with Two Arguments<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Original Function to add two numbers.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def addTwoNumbers(a, b):\n    c=a+b\n    return c\n\nc=addTwoNumber(4, 5)\n \nprint(&quot;Addition of two numbers=&quot;, c)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Addition of two numbers=9<\/pre>\n\n\n\n<p>Now our aim is to modify the behavior of&nbsp;<code>addTwoNumbers()<\/code>&nbsp;without changing function definition and function call.<\/p>\n\n\n\n<p>What function behavior do we want to change?<\/p>\n\n\n\n<p>We want&nbsp;<code>addTwoNumbers<\/code>&nbsp;function should calculate the sum of the square of two numbers instead of the sum of two numbers.<\/p>\n\n\n\n<p>Here is a simple decorator to change the behavior of the existing function.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def decorateFun(func): \n    def sumOfSquare(x, y): \n        return func(x**2, y**2) \n    return sumOfSquare \n\n@decorateFun\ndef addTwoNumbers(a, b): \n    c = a+b \n    return c \n\nc = addTwoNumbers(4,5) \nprint(&quot;Addition of two numbers=&quot;, c)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Addition of two numbers=41<\/pre>\n\n\n\n<p>The below simple program is equivalent to the above decorator example. Here we are changing the function call.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def decorateFun(func): \n    def sumOfSquare(x, y): \n        return func(x**2, y**2) \n    return sumOfSquare \n\ndef addTwoNumbers(a, b): \n    c = a+b \n    return c \n\nobj=decorateFun(addTwoNumbers) \nc=obj(4,5) \nprint(&quot;Addition of square of two numbers=&quot;, c)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Addition of square of two numbers=41<\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The number of arguments to the function inside the decorators should be the same as the number of arguments to the actual function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_to_Use_Python_Decorators\"><\/span>When to Use Python Decorators?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Suppose you are working on a project. You are asked to make the changes in a certain complex function.<\/p>\n\n\n\n<p>If you are making changes in already tested and robust functions, you can not deny the possibility of breaking functionality.<\/p>\n\n\n\n<p>A better way is to use Python decorators.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You don\u2019t need to make changes in the already tested function.<\/li><li>You don\u2019t need to make any changes to the function call. This will be very useful if there are multiple function calls in your project source code.<\/li><\/ul>\n\n\n\n<p>You can also pass the arguments to the decorators. You can modify these arguments inside the decorators before passing them to the original function.<\/p>\n\n\n\n<p>This is all about Python decorators explained with examples. If you have any questions, ask in the comment section below.<\/p>\n\n\n\n<p>After decorators,&nbsp;<a href=\"https:\/\/www.csestack.org\/python-lambda-function-list-comprehension-map-filter-reduce\/\">learn lambda function<\/a>&nbsp;which is another advance topic in Python programming.<\/p>\n\n\n\n<p>Happy Pythoning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Complete Tutorial] Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,73],"tags":[72],"class_list":["post-5799","post","type-post","status-publish","format-standard","hentry","category-code","category-python","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Step-by-step] Python Decorators Explained with Examples for Beginners<\/title>\n<meta name=\"description\" content=\"Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/python-decorators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Step-by-step] Python Decorators Explained with Examples for Beginners\" \/>\n<meta property=\"og:description\" content=\"Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/python-decorators\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-14T07:36:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-11T10:24:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"[Step-by-step] Python Decorators Explained with Examples for Beginners\",\"datePublished\":\"2019-09-14T07:36:45+00:00\",\"dateModified\":\"2022-04-11T10:24:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/\"},\"wordCount\":852,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"image\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Python-Decorators-Explained.png\",\"keywords\":[\"Python\"],\"articleSection\":[\"Code\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/\",\"name\":\"[Step-by-step] Python Decorators Explained with Examples for Beginners\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Python-Decorators-Explained.png\",\"datePublished\":\"2019-09-14T07:36:45+00:00\",\"dateModified\":\"2022-04-11T10:24:18+00:00\",\"description\":\"Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Python-Decorators-Explained.png\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Python-Decorators-Explained.png\",\"width\":474,\"height\":275,\"caption\":\"Python Decorators Explained\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-decorators\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Step-by-step] Python Decorators Explained with Examples for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Step-by-step] Python Decorators Explained with Examples for Beginners","description":"Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators.","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:\/\/www.csestack.org\/python-decorators\/","og_locale":"en_US","og_type":"article","og_title":"[Step-by-step] Python Decorators Explained with Examples for Beginners","og_description":"Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators.","og_url":"https:\/\/www.csestack.org\/python-decorators\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2019-09-14T07:36:45+00:00","article_modified_time":"2022-04-11T10:24:18+00:00","og_image":[{"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png","type":"","width":"","height":""}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/python-decorators\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/python-decorators\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"[Step-by-step] Python Decorators Explained with Examples for Beginners","datePublished":"2019-09-14T07:36:45+00:00","dateModified":"2022-04-11T10:24:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/python-decorators\/"},"wordCount":852,"commentCount":6,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"image":{"@id":"https:\/\/www.csestack.org\/python-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png","keywords":["Python"],"articleSection":["Code","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/python-decorators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/python-decorators\/","url":"https:\/\/www.csestack.org\/python-decorators\/","name":"[Step-by-step] Python Decorators Explained with Examples for Beginners","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.csestack.org\/python-decorators\/#primaryimage"},"image":{"@id":"https:\/\/www.csestack.org\/python-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png","datePublished":"2019-09-14T07:36:45+00:00","dateModified":"2022-04-11T10:24:18+00:00","description":"Python decorators explained with a basic example. Passing arguments and parameters to the Python decorators.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/python-decorators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/python-decorators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/python-decorators\/#primaryimage","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/09\/Python-Decorators-Explained.png","width":474,"height":275,"caption":"Python Decorators Explained"},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/python-decorators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"[Step-by-step] Python Decorators Explained with Examples for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/5799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=5799"}],"version-history":[{"count":21,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/5799\/revisions"}],"predecessor-version":[{"id":9812,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/5799\/revisions\/9812"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=5799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=5799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=5799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}