Sitemap
Jungletronics

Education & STEAM Learning by building. Practical explorations in AI, programming, electronics, robotics, IoT, and other STEAM topics — from ideas and experiments to working products. Follow and build with us.

Decorator in Python

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

3 min readDec 21, 2021

--

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 companiesNow 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!

Press enter or click to view image in full size
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)!

Get J3’s stories in your inbox

Join Medium for free to get updates from this writer.

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 ;)

--

--

Written by J3

Computer Engineer | AI & Data Systems | IoT & Embedded Systems | Product Creator — From Idea to Product | Founder @ Jungletronics & Kidstronics

Published in Jungletronics

Education & STEAM Learning by building. Practical explorations in AI, programming, electronics, robotics, IoT, and other STEAM topics — from ideas and experiments to working products. Follow and build with us.