<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dmusketeer</title>
    <description>The latest articles on DEV Community by Dmusketeer (@dmusketeer).</description>
    <link>https://dev.to/dmusketeer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F701328%2Fc9f42c52-1f66-4d86-8a67-8aed13d02f0a.png</url>
      <title>DEV Community: Dmusketeer</title>
      <link>https://dev.to/dmusketeer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dmusketeer"/>
    <language>en</language>
    <item>
      <title>Decorators in Python</title>
      <dc:creator>Dmusketeer</dc:creator>
      <pubDate>Thu, 17 Mar 2022 15:41:04 +0000</pubDate>
      <link>https://dev.to/dmusketeer/decorators-in-python-22g8</link>
      <guid>https://dev.to/dmusketeer/decorators-in-python-22g8</guid>
      <description>&lt;p&gt;let’s understand what is decorators in python and how it work …&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz4vavnwurcc4psime3vo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz4vavnwurcc4psime3vo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decorators are evolved from the concept of closures.&lt;/li&gt;
&lt;li&gt;A decorator function is a higher order function that takes a function as an argument and returns the inner function.&lt;/li&gt;
&lt;li&gt;A decorator is capable of adding extra functionality to an existing function, without altering it.&lt;/li&gt;
&lt;li&gt;The decorator function is prefixed with @ symbol and written above the function definition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider the below three examples:&lt;/p&gt;

&lt;p&gt;I. First one shows the creation of closure function wish using the higher order function outer.&lt;br&gt;
II. The second one shows the creation of decorator function outer, which is used to decorate function sayHello. This is achieved with a small change to Example1.&lt;br&gt;
III. Third one displays decorating the sayHello function with decorator function, outer, using @ symbol.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4gza5horumbr5g1lqpud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4gza5horumbr5g1lqpud.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;wish is the closure function obtained by calling an outer function with the argument sayHello.&lt;/li&gt;
&lt;li&gt;When wish function is called, inner function gets executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
Accessing : sayHello&lt;br&gt;
Hello!&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filpoqrr5elkhfc2uz3i2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filpoqrr5elkhfc2uz3i2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
Accessing : sayHello&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function returned by outer is assigned to sayHello i.e the function name passed as argument to outer.&lt;/li&gt;
&lt;li&gt;This makes outer a decorator to sayHello.
In Python, decorating a function can also be achieved by writing decorator function name, prefixed with @ symbol, just above the function to be decorated.&lt;/li&gt;
&lt;li&gt;Hence,sayHello = outer(sayHello) expression is same as @outer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example 3:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp4mwv7ui3bybxkbuphr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp4mwv7ui3bybxkbuphr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
Accessing : sayHello &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The intent of this article is to give you enough information about python decorators through examples.&lt;/p&gt;

</description>
      <category>python</category>
      <category>advancepython</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
