Python Decorators: The Complete Guide

preview_player
Показать описание
Python decorators are a great way to add functionality to your Python functions. In this video, I'll show you what they are, how they work, and some of the most useful decorators you can use in your code.

🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund

🔖 Chapters:
0:00 Intro
2:20 Object-Oriented decorator
9:40 Functional decorator
17:17 Functools wraps
21:20 Decorators with arguments
24:03 Should you use decorators?
26:21 Outro

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

The way you progressively take the viewer through the level of abstraction is one of the best parts of this, and many of your videos.

timlind
Автор

I've consumed a lot of programming content recently and this channel is one of the best I've seen. Well produced, professional and perfect pacing. Even the occasional joke!

demolazer
Автор

I cannot overstate how well you described the decorators in every aspect! Great work!

pavfrang
Автор

Hi, Arjan! Can you make a video about "module/package structure" in Python? For instance, using a project as an example, what classes should be grouped together in which modules/packages (nomenclature, subjects, etc). Regards!

m___
Автор

".. a pain in the class.." 🤣 Added to my sayings... Thanks Arjan!

byryepez
Автор

Great stuff. I have never understood decorators... Until now. It was so nice how you showed step by step how it works. I thought decorators were only python thing but now I see its a general concept doable in all languages

petrnovota
Автор

This is just gold on youtube. So many junior devs don't understand decorators in Python, and think of them only as this "Magic" that you put on top without understanding anything. Thank you, Arjan.

shizueigaki
Автор

I came for the high quality and well thought out content. I stayed for it too. But I loved it for all of the glorious puns. :D Thank you for the education and the puns. Both are much appreciated!

lanthos
Автор

One of the best channel for learning python best practices. Thanks alot Arjan!

timelschner
Автор

This dude i can tell came from Java/C++ world he mastered OOP - this is a great for a seasoned developer. Thanks bro.

enockoloo
Автор

I know the decorator pattern from top of my head, but I use python decorators so rarely I have to re-learn them every time :)
Another good use case for decorators is error handling. We catch different exceptions in a web app on different layers and turn them into http errors.
But overall decorators are a niche thing.

banatibor
Автор

Welp. I guess I now know exactly what I need to rewrite part of a work project. There's some complicated and annoying interdependencies that I am not a fan of, but I hadn't figured out how to solve it.

Also, thanks for explaining something that as far as I could tell were just magic!

Melindrea
Автор

It's literally like you're spying on my current challenges at work! There's a heavily nested decorators function that is being used and this has cleared things up a lot! Thanks so much, keep going with the great work!

ashishthanki
Автор

I definitely agree that wrapping logic in decorators makes code harder to read, but I love using them to register handlers, like how Flask defines routes.

I use it like that all the time for event handlers, and I just think it makes the code super easy to read and understand.

Love your videos, so happy to see someone else who loves Python as much as I do 😁

AndreasHejndorf
Автор

Turtles all the way down. Glad you mentioned the "harder to read" downside. Excellent presentation of a fun tool.

robbybobbyhobbies
Автор

This is the one of the most interesting and enlightening video within your playlist.
I am about to flex all these skills from your video into my code-base. 🤣

tienduong
Автор

JIC: we also can implement decorators using class with `__init__` and `__call__` method (`__init__` for parameterizing and `__call__` for applying deco to func of class)

And in addition we can decorate not only functions but classes as well

coppolos
Автор

Decorators are truly a double edged sword. I have used them in the past to pass on a database session through multiple layers of controllers in a rest application. The decorator took ownership of the session and decided when to open / flush / commit / and close the session. It also added some parameters to every controller function in order to implement some security features. It worked wonders for our project as even after countless controller calls a session could seamlessly revert to an old state if an error occurred and controller functions were automatically secured, but to this day it confuses some developers when working with it, because not everyone understands python decorators well. Though I still stand to that design choice today.

Juhujalp
Автор

Omg, thought how to combine music with your videos - and here it comes. Much easier to switch from coding to learning now, nice feature:)

ciqtbwn
Автор

I started watching recently and I have been pleasantly surprised by the quality of your content. There is one caveat, I think, to using the @decorator syntax of python and that is if you need to provide different parameters to the wraper function in different moments. Since the decorator is set at the beginning of the definition of a function, you would either define a function multiple times with the decorator with updated parameters, or use one of the other ways to use a decorator as you showed in the video.

manolito
visit shbcf.ru