{"id":75831,"date":"2020-01-29T09:39:04","date_gmt":"2020-01-29T04:09:04","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75831"},"modified":"2020-01-29T09:39:04","modified_gmt":"2020-01-29T04:09:04","slug":"python-decorators","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/","title":{"rendered":"Python Decorators &#8211; Learn to create and use decorators"},"content":{"rendered":"<p>We have started with <strong>functional programming<\/strong> in <strong>Python<\/strong>. Now let\u2019s move on to an <strong>advanced concept<\/strong> in functional programming.<\/p>\n<p>In this tutorial, we will learn about <strong>decorators<\/strong> in <strong>Python<\/strong> and how to <strong>create<\/strong> and <strong>use<\/strong> Python decorators.<\/p>\n<h3>What are Decorators?<\/h3>\n<p><strong>Python<\/strong> is an <strong>object-oriented language<\/strong> and <strong>everything<\/strong> in <strong>Python<\/strong> is an <strong>object<\/strong>. And therefore, <strong>functions<\/strong> are <strong>first-class objects<\/strong>.<\/p>\n<p>We can <strong>refer<\/strong> to them, <strong>pass<\/strong> them to <strong>variables<\/strong> and <strong>return them<\/strong> from <strong>functions<\/strong>. We can <strong>pass them<\/strong> as <strong>arguments<\/strong> to <strong>higher-order<\/strong> functions and <strong>define<\/strong> them in <strong>functions<\/strong>.<\/p>\n<p><strong>Decorators<\/strong> are like <strong>gift wrappers<\/strong>. A <strong>decorator<\/strong> is a <strong>function<\/strong> that we can use to <strong>modify<\/strong> the <strong>behavior <\/strong>of a <strong>function<\/strong> or a <strong>class<\/strong>.<\/p>\n<p>If you want to <strong>extend<\/strong> the <strong>behavior<\/strong> of a <strong>function<\/strong> but don\u2019t want to <strong>modify<\/strong> it <strong>permanently<\/strong>, you can <strong>wrap a decorator<\/strong> on it. This is called <strong>metaprogramming,<\/strong> as one part of a <strong>program<\/strong> tries to <strong>change<\/strong> another at <strong>compile time<\/strong>.<\/p>\n<p>Some common <strong>decorators<\/strong> in Python are <strong>@property<\/strong>, <strong>@classmethod<\/strong> and <strong>@staticmethod<\/strong>.<\/p>\n<h3>How to Create a Decorator in Python?<\/h3>\n<p>Let\u2019s say we have <strong>nested functions<\/strong>. The outer function takes the <strong>function<\/strong> to <strong>decorate<\/strong> as an <strong>argument<\/strong> and then the <strong>inner <\/strong>function <strong>calls<\/strong> <strong>it<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  def inner():\n    print(\"@@@@@@@@@@@@@@@@\")\n    func()\n    print(\"@@@@@@@@@@@@@@@@\")\n  return inner<\/pre>\n<p>This is the <strong>decorator<\/strong>.<\/p>\n<p>Now let\u2019s define the <strong>function<\/strong> we want to <strong>decorate<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Hi<\/div>\n<p>This <strong>function<\/strong> prints <strong>Hi<\/strong>.<\/p>\n<p>Now if we call<strong> outer<\/strong> with this function <strong>sayhi<\/strong> as an <strong>argument<\/strong> and then <strong>assign<\/strong> this to <strong>sayhi<\/strong>, it <strong>modifies sayhi<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi=outer(sayhi)<\/pre>\n<p>Now calling it <strong>prints<\/strong> <strong>two lines<\/strong> of <strong>\u2018@\u2019<\/strong> around it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<br \/>\nHi<br \/>\n@@@@@@@@@@@@@@@@<\/div>\n<p>You can also give it a <strong>new name<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  def inner():\n    print(\"@@@@@@@@@@@@@@@@\")\n    func()\n    print(\"@@@@@@@@@@@@@@@@\")\n  return inner<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; hello=outer(sayhi)\n&gt;&gt;&gt; hello()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<br \/>\nHi<br \/>\n@@@@@@@@@@@@@@@@<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Hi<\/div>\n<h3>Decorators on Functions that Return Something and Take Arguments<\/h3>\n<p>The <strong>function<\/strong> in the previous example <strong>did not return<\/strong> anything. But what if we want to <strong>decorate a function<\/strong> that <strong>returns<\/strong> a <strong>value<\/strong> or takes <strong>parameters<\/strong>?<\/p>\n<p>Let\u2019s see.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def add(a,b):\n  return a+b<\/pre>\n<p>We have a <strong>function<\/strong> <strong>add<\/strong> that <strong>adds two<\/strong> <strong>numbers<\/strong>. This is the <strong>decorator<\/strong>:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  def inner(a,b):\n    print(\"@@@@@@@@@@@@@@@@\")\n    print(func(a,b))\n    print(\"@@@@@@@@@@@@@@@@\")\n  return inner<\/pre>\n<p>This<strong> decorator\u2019s inner function<\/strong> takes <strong>parameters<\/strong> <strong>a<\/strong> and <strong>b<\/strong>, and then <strong>inside<\/strong>, makes a <strong>call<\/strong> to <strong>func<\/strong> with the <strong>arguments<\/strong> <strong>a<\/strong> and <strong>b<\/strong>. It <strong>prints<\/strong> this then.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; add=outer(add)\n&gt;&gt;&gt; add(2,3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<br \/>\n5<br \/>\n@@@@@@@@@@@@@@@@<\/div>\n<p>If the <strong>inner function<\/strong> calls <strong>func(a,b)<\/strong> instead of <strong>printing<\/strong> it, it <strong>returns None<\/strong> and we only get <strong>two lines<\/strong> of<strong> \u2018@\u2019<\/strong>.<\/p>\n<h3>Structure of Python Decorators<\/h3>\n<p>Let\u2019s see its <strong>syntax<\/strong> again.<\/p>\n<p>We take <strong>nested functions<\/strong> for this. The <strong>outer function<\/strong> defines the <strong>inner function<\/strong> and <strong>returns<\/strong> it too, we <strong>cannot<\/strong> make a <strong>call<\/strong> to it instead of <strong>returning<\/strong> it. If we <strong>call<\/strong> the <strong>inner function<\/strong> instead of <strong>returning<\/strong> it:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  def inner():\n    print(\"@@@@@@@@@@@@@@@@\")\n    func()\n    print(\"@@@@@@@@@@@@@@@@\")\n  inner()<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi=outer(sayhi)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<br \/>\nHi<br \/>\n@@@@@@@@@@@@@@@@<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#58&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>sayhi()<br \/>\nTypeError: &#8216;NoneType&#8217; object is not callable<\/div>\n<p><strong>Applying<\/strong> the <strong>decorator<\/strong> <strong>calls sayhi<\/strong> and <strong>prints<\/strong> the <strong>output<\/strong>. And then <strong>calling sayhi()<\/strong> raises a <strong>TypeError<\/strong> because outer <strong>does not<\/strong> return anything.<\/p>\n<p>If we use only <strong>one function<\/strong> instead of <strong>nested functions<\/strong>:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  print(\"@@@@@@@@@@@@@@@@\")\n  func()\n  print(\"@@@@@@@@@@@@@@@@\")<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi=outer(sayhi)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<br \/>\nHi<br \/>\n@@@@@@@@@@@@@@@@<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#64&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>sayhi()<br \/>\nTypeError: &#8216;NoneType&#8217; object is not callable<\/div>\n<p>And <strong>returning func<\/strong> in outer <strong>does not help<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  print(\"@@@@@@@@@@@@@@@@\")\n  return func\n  print(\"@@@@@@@@@@@@@@@@\")<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi=outer(sayhi)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Hi<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Hi<\/div>\n<h3>Pie Syntax<\/h3>\n<p>If you don\u2019t want to use this line:<\/p>\n<p><strong>sayhi=outer(sayhi)<\/strong>, you can use this <strong>syntactic sugar<\/strong> to attach a <strong>decorator<\/strong> to a <strong>function<\/strong>:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def outer(func):\n  def inner():\n    print(\"@@@@@@@@@@@@@@@@\")\n    func()\n    print(\"@@@@@@@@@@@@@@@@\")\n  return inner<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; @outer\ndef sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">@@@@@@@@@@@@@@@@<br \/>\nHi<br \/>\n@@@@@@@@@@@@@@@@<\/div>\n<h3>Chained Decorators in Python<\/h3>\n<p>You can <strong>apply<\/strong> <strong>more<\/strong> than <strong>one decorator<\/strong> to a <strong>function<\/strong> or <strong>class<\/strong>. The <strong>order<\/strong> in which you apply them <strong>matters<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def decor1(func):\n  def inner():\n    print('11111111111111111')\n    func()\n    print('11111111111111111')\n  return inner<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def decor2(func):\n  def inner():\n    print('22222222222222222')\n    func()\n    print('22222222222222222')\n  return inner<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; @decor1\n@decor2\ndef sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">11111111111111111<br \/>\n22222222222222222<br \/>\nHi<br \/>\n22222222222222222<br \/>\n11111111111111111<\/div>\n<p>In this example, we have two decorators &#8211; <strong>decor1<\/strong> and <strong>decor2<\/strong>.<\/p>\n<p><strong>decor1<\/strong> is <strong>applied first<\/strong> and then <strong>decor2<\/strong> is <strong>applied<\/strong>.<\/p>\n<p>For this, we just write <strong>@decor1<\/strong> and <strong>@decor2<\/strong> in <strong>separate lines<\/strong>.<\/p>\n<p>So in the output, <strong>11111111111111111<\/strong> is first and then <strong>22222222222222222<\/strong>.<\/p>\n<p>The <strong>order<\/strong> in which you <strong>apply<\/strong> the <strong>decorators<\/strong> matters.<\/p>\n<h3>Python Decorators with Arguments<\/h3>\n<p><strong>Decorators<\/strong> can take <strong>arguments<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def decor1(func, num):\n  def inner():\n    print('1'*num)\n    func()\n    print('1'*num)\n  return inner<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print('Hi')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sayhi=decor1(sayhi, num=9)\n&gt;&gt;&gt; sayhi()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">111111111<br \/>\nHi<br \/>\n111111111<\/div>\n<p>Here, we make <strong>decor1 print<\/strong> the number <strong>\u20181\u2019<\/strong> <strong>9<\/strong> <strong>times<\/strong> on <strong>each side<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>In this TechVidvan&#8217;s Python decorators article, we learned about the <strong>decorator functions<\/strong> in <strong>Python<\/strong> and then we saw how to <strong>create<\/strong>, <strong>use<\/strong> and <strong>chain<\/strong> them.<\/p>\n<p>We learned about the <strong>structure<\/strong>, <strong>pie syntax<\/strong>, <strong>Python decorators<\/strong> with <strong>arguments<\/strong> and <strong>decorators<\/strong> on <strong>functions<\/strong> that return a <strong>value<\/strong> or <strong>take arguments<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have started with functional programming in Python. Now let\u2019s move on to an advanced concept in functional programming. In this tutorial, we will learn about decorators in Python and how to create and&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75960,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1486,1487,1488,1489,1490,1491,1492,1493],"class_list":["post-75831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-chained-decorators-in-python","tag-decorators-in-python","tag-pie-syntax","tag-python-decorator-with-arguments","tag-python-decorators","tag-structure-of-python-decorators","tag-what-are-decorators-in-python","tag-what-are-python-decorators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Decorators - Learn to create and use decorators - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn the advanced concept in functional programming, that is, Python decorators. Also, learn to create a decorator in Python, its structure, arguments, etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-decorators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Decorators - Learn to create and use decorators - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn the advanced concept in functional programming, that is, Python decorators. Also, learn to create a decorator in Python, its structure, arguments, etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-decorators\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-29T04:09:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Python-decorators.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Decorators - Learn to create and use decorators - TechVidvan","description":"Learn the advanced concept in functional programming, that is, Python decorators. Also, learn to create a decorator in Python, its structure, arguments, etc.","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:\/\/techvidvan.com\/tutorials\/python-decorators\/","og_locale":"en_US","og_type":"article","og_title":"Python Decorators - Learn to create and use decorators - TechVidvan","og_description":"Learn the advanced concept in functional programming, that is, Python decorators. Also, learn to create a decorator in Python, its structure, arguments, etc.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-29T04:09:04+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Python-decorators.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Decorators &#8211; Learn to create and use decorators","datePublished":"2020-01-29T04:09:04+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/"},"wordCount":719,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Python-decorators.jpg","keywords":["Chained Decorators in Python","decorators in python","Pie Syntax","python decorator with arguments","python decorators","Structure of Python Decorators","what are decorators in python","what are python decorators"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-decorators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/","url":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/","name":"Python Decorators - Learn to create and use decorators - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Python-decorators.jpg","datePublished":"2020-01-29T04:09:04+00:00","description":"Learn the advanced concept in functional programming, that is, Python decorators. Also, learn to create a decorator in Python, its structure, arguments, etc.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-decorators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Python-decorators.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Python-decorators.jpg","width":802,"height":420,"caption":"decorators in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-decorators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Decorators &#8211; Learn to create and use decorators"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75831"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75831\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75960"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}