Akshay ๐Ÿš€
Akshay ๐Ÿš€

@akshay_pachaar

9 Tweets 3 reads Sep 18, 2023
Decorators are one of the most powerful feature of Python! ๐Ÿ”ฅ
However, understanding them can be a bit overwhelming!
Today, I'll clearly explain how decorators work!
Let's go! ๐Ÿš€
Before we jump onto decorator, we must understand that functions in python are "first-class" objects!
It means that a function can be:
- passed around as an argument
- used in expressions
- returned as values of other functions
Just like integers or string!
Check this out๐Ÿ‘‡
To get the essence of Decorators, let's imagine it's your friend's birthday, you're giving them a gift.
But before you do, you wrap it in a fancy gift paper to enhance its look, right?
Decorators do the same thing but with functions. โœจ
Let's see how ... ๐Ÿ‘‡
Decorators are a bit like wrapping paper.
They're functions that take another function, add some functionality, and return the new "decorated" function.
Check this out๐Ÿ‘‡
The syntax we saw above is a bit clumsy.
However Python has a subtle way to do it! ๐Ÿ’ซ
We can use the `@` symbol along with the decorator name right before the function definition.
It does exactly the same thing as before but looks cleaner!โœจ
Check this out๐Ÿ‘‡
Now if you are wondering, what happens when my function takes an argument!
Here'a an illustration of how decoration works when the function being greeted takes an argument!
Check this out๐Ÿ‘‡
Decorators are powerful and flexible.
They can be used to:
- log function calls
- check function arguments
- run code before and after functions,
- and much more ...
They allow us to add functionality in a clean, consistent way.
Here's an interesting & generic use-case ๐Ÿ‘‡
Now, I would like to give a shoutout to
PicWish for sponsoring this post and supporting my work! ๐Ÿ™
PicWish is an AI powered platform that let's you:
- Remove background
- Unblur Images
- An much more ...
Check this out๐Ÿ‘‡
picwish.com
That's a wrap!
If you interested in:
- Python ๐Ÿ
- Data Science ๐Ÿ“ˆ
- Machine Learning ๐Ÿค–
- Maths for ML ๐Ÿงฎ
- MLOps ๐Ÿ› 
- CV/NLP ๐Ÿ—ฃ
- LLMs ๐Ÿง 
Find me โ†’ @akshay_pachaar โœ”๏ธ
Subscribe to my Newsletterโ†’mlspring.beehiiv.com
Everyday, I share tutorials on above topics!
Cheers!

Loading suggestions...