avatarLiu Zuo Lin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2769

Abstract

, and returns another function.</p><div id="d3fb"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">add_exclamation</span>(<span class="hljs-params">function</span>): <span class="hljs-keyword">def</span> <span class="hljs-title function_">inner</span>(<span class="hljs-params">*args</span>): <span class="hljs-keyword">return</span> function(*args) + <span class="hljs-string">'!'</span> <span class="hljs-keyword">return</span> inner

<span class="hljs-meta">@add_exclamation</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>): <span class="hljs-keyword">return</span> <span class="hljs-string">'hello '</span> + name

<span class="hljs-built_in">print</span>(greet(<span class="hljs-string">'tim'</span>)) <span class="hljs-comment"># hello tim!</span></pre></div><p id="ef10">^ Our decorator and decorated function in action.</p><div id="a321"><pre><span class="hljs-meta">@add_exclamation</span> <span class="hljs-meta">@add_exclamation</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>): <span class="hljs-keyword">return</span> <span class="hljs-string">'hello '</span> + name

<span class="hljs-built_in">print</span>(greet(<span class="hljs-string">'tim'</span>)) <span class="hljs-comment"># hello tim!!</span></pre></div><p id="f992">^ Note that we can decorate a function more than once.</p><h1 id="5b4b">How This Works</h1><div id="63fa"><pre><span class="hljs-meta">@add_exclamation</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>): <span class="hljs-keyword">return</span> <span class="hljs-string">'hello '</span> + name

<span class="hljs-comment"># THIS IS THE SAME AS</span>

greet = add_exclamation(greet)</pre></div><div id="88a8"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">add_exclamation</span>(<span class="hljs-params">function</span>): <span class="hljs-keyword">def</span> <span class="hljs-title function_">inner</span>(<span class="hljs-params">*args</span>): <span class="hljs-keyword">return</span> function(*args) + <span class="hljs-string">'!'</span> <span class="hljs-keyword">return</span> inner

<span class="hljs-comment"># Tracing add_exclamation(greet)</span>

function = greet

<span class="hljs-comment"># inner becomes</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">inner</span>(<span class="hljs-params">*args</span>): <span class="hljs-keyword">return</span> greet(*args) + <span class="hljs-string">'!'</span>

<span class="hljs-comment"># here is where

Options

the ! is added</span>

<span class="hljs-comment"># inner is then returned</span></pre></div><div id="5383"><pre><span class="hljs-comment"># Essentially</span>

greet = inner

<span class="hljs-comment"># where inner is</span>

<span class="hljs-keyword">def</span> <span class="hljs-title function_">inner</span>(<span class="hljs-params">*args</span>): <span class="hljs-keyword">return</span> greet(*args) + <span class="hljs-string">'!'</span></pre></div><p id="78f1">And that is how decorators work on a very basic level.</p><h1 id="2d7b">Conclusion</h1><p id="6582">Hope this was clear and helpful</p><h1 id="7364">Some Final words</h1><p id="092d"><i>If this story provided value to you, and you wish to show support, you could:</i></p><ol><li><i>Clap multiple times for this story (this really helps me out!)</i></li><li><i>Consider signing up for a Medium membership using my link — it’s $5 per month and you get to read unlimited stories on Medium.</i></li></ol><p id="2aa5"><a href="https://zlliu.medium.com/membership"><b><i>Sign up using my link here to read unlimited Medium articles.</i></b></a></p><p id="8110"><b>Get my free Ebooks: <a href="https://zlliu.co/books">https://zlliu.co/books</a></b></p><p id="6386"><i>I write Python articles (sometimes other stuff) that the younger me would have wanted to read. Do join my email list to get notified whenever I publish.</i></p><div id="1340" class="link-block"> <a href="https://zlliu.medium.com/subscribe"> <div> <div> <h2>Get an email whenever Liu Zuo Lin publishes.</h2> <div><h3>Get an email whenever Liu Zuo Lin publishes. By signing up, you will create a Medium account if you don't already have…</h3></div> <div><p>zlliu.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*GfSWvlhVbjvYMhKH)"></div> </div> </div> </a> </div><p id="53ed"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>.</i></p><p id="4b6b"><i>Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a></i>, <a href="https://www.linkedin.com/company/inplainenglish/"><b><i>LinkedIn</i></b></a><i>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>YouTube</b></a>, and <a href="https://discord.gg/GtDtUAvyhW"><b>Discord</b></a><b>.</b></i></p><p id="f310"><b><i>Interested in scaling your software startup</i></b><i>? Check out <a href="https://circuit.ooo?utm=publication-post-cta"><b>Circuit</b></a>.</i></p></article></body>

Decorators In Python & How To Understand Them

# A step by step walkthrough

@something
def your_function(a, b):
  # stuff

^ You’ve probably seen something like this. The @something on top of our function is known as a decorator.

What The @ Means

Let’s say we have a simple function and a simple decorator.

@my_decorator
def my_function(x):
  # stuff

This is the exact same as the following code:

def my_function(x):
  # stuff

my_function = my_decorator(my_function)

The @ simply provides us a way to make our code look more pretty. But what it is actually doing is in the second code block.

What a Decorator is

A decorator is a function that:

  1. Takes in a function
  2. Returns another function

Decorators are used to change how a function behaves.

def greet(name):
  return 'hello ' + name

^ Here, we have a simple greet function.

def add_exclamation(function):
  def inner(*args):
    return function(*args) + '!'
  return inner

# ^ this function is a decorator function
# it adds one ! after its input function's return value

^ and here, we have a simple decorator. Remember, a decorator itself is a function. It just takes in a function, and returns another function.

def add_exclamation(function):
  def inner(*args):
    return function(*args) + '!'
  return inner

@add_exclamation
def greet(name):
  return 'hello ' + name

print(greet('tim'))  # hello tim!

^ Our decorator and decorated function in action.

@add_exclamation
@add_exclamation
def greet(name):
  return 'hello ' + name

print(greet('tim'))  # hello tim!!

^ Note that we can decorate a function more than once.

How This Works

@add_exclamation
def greet(name):
  return 'hello ' + name

# THIS IS THE SAME AS

greet = add_exclamation(greet)
def add_exclamation(function):
  def inner(*args):
    return function(*args) + '!'
  return inner

# Tracing add_exclamation(greet)

function = greet

# inner becomes
def inner(*args):
  return greet(*args) + '!'

  # here is where the ! is added

# inner is then returned
# Essentially

greet = inner

# where inner is

def inner(*args):
  return greet(*args) + '!'

And that is how decorators work on a very basic level.

Conclusion

Hope this was clear and helpful

Some Final words

If this story provided value to you, and you wish to show support, you could:

  1. Clap multiple times for this story (this really helps me out!)
  2. Consider signing up for a Medium membership using my link — it’s $5 per month and you get to read unlimited stories on Medium.

Sign up using my link here to read unlimited Medium articles.

Get my free Ebooks: https://zlliu.co/books

I write Python articles (sometimes other stuff) that the younger me would have wanted to read. Do join my email list to get notified whenever I publish.

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Interested in scaling your software startup? Check out Circuit.

Python
Python Programming
Recommended from ReadMedium