avatarJ3

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

3041

Abstract

(<span class="hljs-string">"I need a Decorator!"</span>)</span></pre></div><p id="6212">This function is desperately in need of an upgrade. Let´s suppose it needs some code before and after its actual output.</p><p id="f2ac">There is no reimplementation options. The system must not stop.</p><p id="041f">This code must always run even after your implementation:</p><div id="7385"><pre><span class="hljs-function"><span class="hljs-title">func_needs_decorator1</span><span class="hljs-params">()</span></span></pre></div><div id="410d"><pre><span class="hljs-selector-tag">I</span> need <span class="hljs-selector-tag">a</span> Decorator!</pre></div><p id="3381">Here comes the Python’s <i>decorator patterns</i> to rescue us:</p><div id="ff48"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">new_decorator</span>(<span class="hljs-params">func</span>)<span class="hljs-symbol">:</span></pre></div><div id="34c5"><pre> <span class="hljs-variable">def</span> <span class="hljs-function"><span class="hljs-title">wrap_func</span>(): <span class="hljs-title">print</span>(<span class="hljs-string">"#####Decorator before...#######"</span>)</span> <span class="hljs-function"><span class="hljs-title">func</span>()</span> <span class="hljs-function"><span class="hljs-title">print</span>(<span class="hljs-string">"#####Decorator after ...#######"</span>)</span> <span class="hljs-variable">return</span> <span class="hljs-variable">wrap_func</span></pre></div><p id="7660">Now, 🎉 Feel The Magic ✨</p><div id="c036"><pre><span class="hljs-variable">func_needs_decorator</span> = <span class="hljs-function"><span class="hljs-title">new_decorator</span>(<span class="hljs-variable">func_needs_decorator1</span>)</span> <span class="hljs-function"><span class="hljs-title">func_needs_decorator</span>()</span></pre></div><div id="c44b"><pre>#####Decorator before...####### I need a Decorator! #####Decorator after ...#######</pre></div><p id="3f39">There you have it!</p><p id="9a2d">Wait…Let’s make in the <i>pythonic way </i>(it reads: code that is readable and beautiful)!</p><p id="ad98">Decorate your methods like this:</p><div id="76b8"><pre><span class="hljs-variable">@new_decorator</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">func_needs_decorator2</span>(): print(<span class="hljs-string">"I need a Decorator!"</span>)</pre></div><p id="3a48">Now type:</p><div id="852f"><pre><span class="hljs-function"><span class="hljs-title">func_needs_decorator2</span><span class="hljs-params">()</span></span></pre></div><div id="c6d5"><pre>#####Decorator before...####### I need a Decorator! #####Decorator after ...#######</pre></div><p id="f915">This code is Beautful, isn’t it?</p><p id="0e22">Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.</p><p id="15e4">That’s all, folks!</p><p id="5ae1">See you soon o/</p><p id="14a9">Bye!</p><p

Options

id="1df9">👉Jupiter notebook <a href="https://drive.google.com/drive/folders/1ydIqB-8HOgRDes11vHQgsrAqj4_F1TqW?usp=sharing">link</a> :)</p><p id="be62">👉<a href="https://github.com/giljr/my_jupyter_notebook">git</a></p><p id="7fb9">👉<a href="https://replit.com/@jaythree/Decoratorfunc">replit</a></p><h1 id="c339">Credits And References</h1><p id="88ad"><b>Jose Portilla </b><a href="https://www.udemy.com/course/python-for-data-science-and-machine-learning-bootcamp/">Python for Data Science and Machine Learning Bootcamp </a>— Learn how to use NumPy, Pandas, Seaborn, Matplotlib, Plotly, Scikit-Learn, Machine Learning, Tensorflow, and more!</p><p id="1b3c"><b>Python <a href="https://www.artima.com/weblogs/viewpost.jsp?thread=240808">Decorators I: Introduction to Python Decorators</a></b> by <a href="https://www.bruceeckel.com/">Bruce Eckel</a></p><p id="3c92"><b>Python <a href="https://www.artima.com/weblogs/viewpost.jsp?thread=240845">Decorators II: Decorator Arguments</a> </b>by <a href="https://www.bruceeckel.com/">Bruce Eckel</a></p><h1 id="9e43">Related Posts</h1><p id="1399"><b>00</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/lambda-in-python-421b0c18e825"><b>Lambda in Python </b></a>— Python Lambda Desmistification</p><p id="dccf"><b>01</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/send-emails-using-python-jupyter-notebook-94d14a5a5655"><b>Send Email in Python</b></a> — Using Jupyter Notebook — How To Send Gmail In Python</p><p id="80e3"><b>02</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/automate-your-email-marketing-with-python-f0d68234b789"><b>Automate Your Email With Python & Outlook</b></a><b> </b>— How To Create An Email Trigger System in Python</p><p id="ec90"><b>03</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/manipulating-files-with-python-3f9a781287e9"><b>Manipulating Files With Python</b></a> — Manage Your Lovely Photos With Python!</p><p id="92c5"><b>04</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/pandas-dataframe-advanced-48f83a5b097f"><b>Pandas DataFrame Advanced </b></a>— A Complete Notebook Review</p><p id="9403"><b>05</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/is-this-leap-year-python-calendar-3d1a61f2c4a7"><b>Is This Leap Year? Python Calendar</b> </a>— How To Calculate If The Year Is Leap Year and How Many Days Are In The Month</p><p id="fdaf"><b>06</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/list-comprehension-in-python-c22c4b0a6a8a"><b>List Comprehension In Python </b></a>— Locked-in Secrets About List Comprehension</p><p id="ba58"><b>07</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/graphs-in-python-b7d243737b77"><b>Graphs — In Python </b></a>— Extremely Simple Algorithms in Python</p><p id="6767"><b>08</b>#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function (this one)</p><blockquote id="a725"><p>edited: Nov, 2022 — add replit link ;)</p></blockquote></article></body>

Decorator in Python

How To Simplifying Your Code And Boost Your Function — #PurePythonSeries — Episode #08

See this scenario:

A Compatibility issues come up when users are using the same type of software for a task, such as word processors, that cannot communicate with each other. This could be due to a difference in their versions or because they are made by different companies
Now you are called to deal with this problem and bring a solution, and all you have is Python!(thanks god:)
You need to change the behavior of a function without modifying the function itself so the system doesn't break:/

Let´s simulate some method to simplifing your task, and brings the point home: Decorator!

Image from this link

Suppose you have this legacy code:

def func_needs_decorator1():
    print("I need a Decorator!")

This function is desperately in need of an upgrade. Let´s suppose it needs some code before and after its actual output.

There is no reimplementation options. The system must not stop.

This code must always run even after your implementation:

func_needs_decorator1()
I need a Decorator!

Here comes the Python’s decorator patterns to rescue us:

def new_decorator(func):
    def wrap_func():
        print("#####Decorator before...#######")
        func()
        print("#####Decorator after ...#######")
     return wrap_func

Now, 🎉 Feel The Magic ✨

func_needs_decorator = new_decorator(func_needs_decorator1)
func_needs_decorator()
#####Decorator before...#######
I need a Decorator!
#####Decorator after ...#######

There you have it!

Wait…Let’s make in the pythonic way (it reads: code that is readable and beautiful)!

Decorate your methods like this:

@new_decorator
def func_needs_decorator2():
    print("I need a Decorator!")

Now type:

func_needs_decorator2()
#####Decorator before...#######
I need a Decorator!
#####Decorator after ...#######

This code is Beautful, isn’t it?

Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.

That’s all, folks!

See you soon o/

Bye!

👉Jupiter notebook link :)

👉git

👉replit

Credits And References

Jose Portilla Python for Data Science and Machine Learning Bootcamp — Learn how to use NumPy, Pandas, Seaborn, Matplotlib, Plotly, Scikit-Learn, Machine Learning, Tensorflow, and more!

Python Decorators I: Introduction to Python Decorators by Bruce Eckel

Python Decorators II: Decorator Arguments by Bruce Eckel

Related Posts

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification

01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function (this one)

edited: Nov, 2022 — add replit link ;)

Python3
Python Decorators
How To
Magic
Jupyter Notebook
Recommended from ReadMedium