Python Decorators: The Complete Guide

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.
Git repository: github.com/ArjanCodes/2023-de...
✍🏻 Take a quiz on this topic: www.learntail.com/quiz/iufnfs
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
💻 ArjanCodes Blog: www.arjancodes.com/blog
🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Designer Mindset Team Packages: www.arjancodes.com/sas
The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
The 30-Day Design Challenge: www.arjancodes.com/30ddc
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
🎓 Sign up to Brilliant now and get 20% off the annual subscription: brilliant.sjv.io/arjancodes.
👍 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!
💬 Discord: discord.arjan.codes
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
📱Instagram: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 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!

Пікірлер: 235

  • @timlind3129
    @timlind3129 Жыл бұрын

    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.

  • @pramodjingade6581

    @pramodjingade6581

    Жыл бұрын

    Yep, exactly!

  • @klmcwhirter

    @klmcwhirter

    11 ай бұрын

    Agreed. While I really enjoy James Powell videos like the one below, @ArjanCodes just presents things in a very fluid manner. kzread.info/dash/bejne/lX-EzrKDqbTYpJc.html My colleagues were lost with the content presented in that James Powell video. But I hope that no one was lost with what @ArjanCodes presented in this one. Good job @ArjanCodes! Thank you.

  • @demolazer
    @demolazer Жыл бұрын

    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!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks so much!

  • @m_7_1_45
    @m_7_1_45 Жыл бұрын

    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!

  • @ErikS-

    @ErikS-

    Жыл бұрын

    I totally support this idea! For me it is still a bit of a mystery how to BEST create a library/package from your own files...

  • @kevon217

    @kevon217

    Жыл бұрын

    ^

  • @Fritz0id

    @Fritz0id

    Жыл бұрын

    Yes x1000. While the syntax isn’t too hard to get my head around, best practices is always difficult to get a handle on. And most info on web is for open source projects. It’s actually quite difficult to get an idea of how to robustly share internal packages with the rest of your team in a non-open way.

  • @manuelstausberg8923

    @manuelstausberg8923

    Жыл бұрын

    +999 to this :)

  • @yasamankarimi8219

    @yasamankarimi8219

    Жыл бұрын

    agreed. i'd like to see that too

  • @byryepez
    @byryepez Жыл бұрын

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

  • @pavfrang
    @pavfrang2 күн бұрын

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

  • @petrnovota8238
    @petrnovota8238 Жыл бұрын

    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

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad you liked it!!

  • @MJ-xl5jz

    @MJ-xl5jz

    Жыл бұрын

    ​"@ArjanCodes" It even works on KZread. I'm just trying to wrap Arjan's Glad-you-liked-it to decorate my comment.

  • @shizueigaki702
    @shizueigaki70210 ай бұрын

    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.

  • @ArjanCodes

    @ArjanCodes

    9 ай бұрын

    Thank you!

  • @jphvnet
    @jphvnet Жыл бұрын

    Really nice video! I discovered Decorators in my new job (C++), but we use the traditional pattern because we can change the order and list of them by the DI configuration tool or inside factories. Amazing how things like anticipation, sync or cached operations are decoupled from the model object.

  • @timelschner8451
    @timelschner8451 Жыл бұрын

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

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you, Tim, glad you find the content helpful!

  • @Shaft0
    @Shaft0 Жыл бұрын

    THANK YOU 😊 Wanted something on this topic and just in time for lunch break today ! Gonna use it immediately

  • @enockoloo3814
    @enockoloo38145 ай бұрын

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

  • @ashishthanki4568
    @ashishthanki4568 Жыл бұрын

    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!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you and will do!

  • @infinikeys
    @infinikeys Жыл бұрын

    I have watched a few videos on this topic and you explained the best. It's so easy to understand with some background (classic decorator pattern in OOP and then how Python gets it done). Thank you.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad it was helpful!

  • @manolito2589
    @manolito25895 ай бұрын

    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.

  • @banatibor83
    @banatibor83 Жыл бұрын

    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.

  • @user-ci9qt2bw9n
    @user-ci9qt2bw9n Жыл бұрын

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

  • @nomex9829
    @nomex98297 ай бұрын

    Great video. For anyone wanting to use the with_default_logging decorator, be aware of a potential issue. Most likely, the logger object will have the name from the module where it was instantiated. If you import it via decorator into another module, it might get confusing where the logging call actually came from because the logger name can be mistaken for the module name. I definitely had the issue in the past and it took me a while to figure out what was going on.

  • @AndreasHejndorf
    @AndreasHejndorf Жыл бұрын

    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 😁

  • @sadhlife

    @sadhlife

    Жыл бұрын

    BTW, flask's decorator functions also return the original function unchanged. So even though a function is a flask route, you can call it just like a regular function if needed. Flask's types were broken regarding this fact, and I had to fix them sometime last year.

  • @Melindrea
    @Melindrea Жыл бұрын

    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!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    You're welcome, Marie! :)

  • @robbybobbyhobbies
    @robbybobbyhobbies Жыл бұрын

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

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you, glad you enjoyed it!

  • @jimmyf.x.9526
    @jimmyf.x.9526 Жыл бұрын

    I liked the first part of your tutorial. It reminded me how I used to decorator before java 1.5, and instantly boosted my learning speed.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks for sharing!

  • @petrnovota8238
    @petrnovota8238 Жыл бұрын

    In my previous work, VW group, decorators were used to decorate classes and decorators created instances of classes that we wanted to use in the class. So they were some kind of Factories for other classes. I had no idea how that worked at the time :)

  • @sounishnath513
    @sounishnath513 Жыл бұрын

    Just way awesome than any video. Thank you loads Arjan ❤

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you so much 😀

  • @MedievalChips
    @MedievalChips Жыл бұрын

    Great video as always, I use the built in decorators but Ive never reach the point of making my own. It looks awesome but in practice I feel its a bit convoluted as an end user. I will definitely will try them out now. Thank you for the great video.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad you liked the video! 😊

  • @loweryjk
    @loweryjk Жыл бұрын

    I wrote a decorator for a web application once that enforced authorization. It would get the user session object from the coroutine context and verify that the user possessed the permissions defined in the decorator call. e.g. @secure('write_permission') Great videos btw! I would appreciate a video on decorator classes. I've always found them very hard to understand how they work and what the pros/cons are. Oh, and a video on the coroutine context! Thanks!

  • @coppolos
    @coppolos Жыл бұрын

    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

  • @liquidpebbles
    @liquidpebbles Жыл бұрын

    Thanks for the lesson Arjan. Very well done.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad you liked it!

  • @lanthos
    @lanthos Жыл бұрын

    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!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad you enjoyed it!

  • @furiousjanush5776
    @furiousjanush577610 ай бұрын

    Amazing content! Thanks so much for sharing that.

  • @mzahran001
    @mzahran001 Жыл бұрын

    Thank you so much for the videos! Could you use more visuals (drawings) before writing the code? I guess if you draw what you are trying to build before building it, this will deliver the message perfectly

  • @swilwerth
    @swilwerth Жыл бұрын

    This is the most practical advise on decorators i've ever seen or heard.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks!

  • @user-mi2bb8bm6s
    @user-mi2bb8bm6s Жыл бұрын

    I'm learning from you a lot. Many thanks!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    My pleasure!

  • @cetilly
    @cetilly9 ай бұрын

    Easily the best presentation on decorators I’ve ever seen. 👏🏻

  • @ArjanCodes

    @ArjanCodes

    9 ай бұрын

    Wow! Thanks so much 😊

  • @guilhem2870
    @guilhem28704 ай бұрын

    cette chaine est une mine d'or ! 🧡

  • @MicheleHjorleifsson
    @MicheleHjorleifsson Жыл бұрын

    One of your best videos yet

  • @Juhujalp
    @Juhujalp Жыл бұрын

    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.

  • @Midlifecrypto
    @Midlifecrypto Жыл бұрын

    Exactly the video I was looking for. 🎉🎉

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad I could help!

  • @KonradG888
    @KonradG888 Жыл бұрын

    thanks this super insightful guide! much appreciated

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you!

  • @nathanmiddleton1478
    @nathanmiddleton1478 Жыл бұрын

    My python code always looks like it's straight out of the 90s and yours reminds me of something from another time completely. Definitely should brush up more :)

  • @trustmub1
    @trustmub1 Жыл бұрын

    I had a hard time understanding decorators in my high school days. Never created my own, but I think I might have to reconsider now

  • @GigaMarou
    @GigaMarou Жыл бұрын

    great video, thx a million, making me revisit those ideas, i tend to forget ;)

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you, glad you liked it!

  • @nayelnoorani
    @nayelnoorani3 ай бұрын

    Thanks for the excellent and easy-to-follow explanation! Brilliant teaching :)

  • @ArjanCodes

    @ArjanCodes

    3 ай бұрын

    I'm glad to hear that the video was helpful :)

  • @kristinnagust9342
    @kristinnagust93426 ай бұрын

    Liked the video when I saw the like video button light up as soon as you talked about it. Nice!

  • @XRay777
    @XRay777 Жыл бұрын

    I recently implemented a version elevator system using decorators. The version elevators were just simple functions that take in a config object and modify it in-place updating it to the next highest version. The decorators were needed because some versions needed multiple mending functions due to inconsistend versioning plus we also had some functions that were just there to provide some convenience options in the configs. As such, the decorator would take the version for which the mending function should be applied as an argument or None it is a convenience thing. The decorator would then take care of adding logging and registering the mending function. Now, outdated configs can be updated by just running all the version escalators on it in the given order, e.g. if the config is version 3 and the current version is 5 it would be 3->4->5 and then run all of the convenience stuff. I think the decorators make the code quite tidy since all you see is really only the migration logic. I would recommend using the class-based approach with `__call__` when you want decorators that take arguments though. I find them easier to read than three nested functions and it makes dealing with defaults easier as well.

  • @joaopedrorocha5693
    @joaopedrorocha5693 Жыл бұрын

    Realized now that Arjan codes is one of the only channels that when you ask for a like i actually stop and like the video. Thought goes like: "that's so useful, it really deserves a like" hehe btw: i would like to see your deep dive on the import system ...

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks for your support! I noted your suggestion :)

  • @kquat7899
    @kquat7899 Жыл бұрын

    This channel has the most useful python tutorials.

  • @Skaypegote
    @Skaypegote Жыл бұрын

    I just started getting into decorators, and this is such a comprehensive guide on what the hell a decorator is, thank you so much

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    You're so welcome!

  • @TimLauridsen
    @TimLauridsen Жыл бұрын

    Great video, I use decorators all the time, but only the cases as you show in the video benchmarking and logging start of end of a method call. Never use multiple decorators, if i need both timing and logging, i create a single decorator that does both

  • @dane2565

    @dane2565

    Жыл бұрын

    this is not always the case. there are quite well known frameworks which use stacked decorators frequently as middleware.

  • @dane2565

    @dane2565

    Жыл бұрын

    also order matters. if you have a number of decorators, do you want to write a decorator for every combination of them?

  • @vladimirtsarapkin2840
    @vladimirtsarapkin2840 Жыл бұрын

    Easy to follow and very helpfull!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Great to hear!

  • @Yankzy
    @Yankzy Жыл бұрын

    Huge pain in the 'class' haha! Very nice!

  • @j_r28
    @j_r28 Жыл бұрын

    One of the best python video makers ever.. Keep going sir.. 😁 I have been following since so long, I'm a machine learning engineer and I come here once in a while, and always watch the latest ones to be sure my python skills are state of the art 😎

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you so much for your support!

  • @diegocastaneda4325
    @diegocastaneda43258 ай бұрын

    Simply an amazing video 😌 Thank you.

  • @ArjanCodes

    @ArjanCodes

    8 ай бұрын

    Glad you enjoyed it, Diego!

  • @aminramazanifar9743
    @aminramazanifar9743 Жыл бұрын

    An amazing tutorial! Thanks

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    You're welcome!

  • @estebanmarco8755
    @estebanmarco8755 Жыл бұрын

    As a functional programmer I love the functional decorator, combinators are good.

  • @lmmartinez97
    @lmmartinez977 ай бұрын

    For me the option that makes most sense is to use functions in the decorator design pattern. That way, if you're benchmarking code (say, in a jupyter environment) you can called the decorated functions of methods, but when you're deploying the code in a script that needs to run for hours and no one will look at, you can call the function without the decorator, thus leaving out a bunch of logging and calls to perf_time()

  • @alexandermartens192
    @alexandermartens192 Жыл бұрын

    Finally understand how decorators work in detail

  • @ChongFrisbee
    @ChongFrisbee Жыл бұрын

    I really like functional programming and specially haskell. I often try to see how to reproduce haskell behavior in python for fun (and sometimes profit). Decorators were the obvious choice to implement currying. The not so obvious choice used by functools.partial (class) is much better, but requires more work to work properly.

  • @DiRo0566
    @DiRo05662 ай бұрын

    Thanks, i like the path from classic patter to python’s decorator style.

  • @ArjanCodes

    @ArjanCodes

    2 ай бұрын

    Glad you like it!

  • @wuzixiao1
    @wuzixiao16 күн бұрын

    Thanks. It is a great video explaining how decorator works. It is a good feature of dynamic programming language.

  • @ArjanCodes

    @ArjanCodes

    6 күн бұрын

    Thank you for the kind words! Glad you liked the video.

  • @avnerduchovni6675
    @avnerduchovni6675 Жыл бұрын

    Thanks Arjan!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    You’re welcome!

  • @Casimistico
    @Casimistico Жыл бұрын

    Superb, like always.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks so much!

  • @sadhlife
    @sadhlife Жыл бұрын

    I was hoping that you would talk about ParamSpec near the end to be able to preserve the argument types of decorated functions for better autocomplete :)

  • @benjaminbertincourt5259
    @benjaminbertincourt5259 Жыл бұрын

    One notable case where I built my own decorators was to build a mini-ETL framework. I was using decorators to inject data validation checks pre and post transformation steps. Also reused the @nested_dataclass trick someone posted online to be able to create dataclasses with nested dataclasses members off of unpacked dictionaries.

  • @manuelstausberg8923

    @manuelstausberg8923

    Жыл бұрын

    Do you have a link for that by chance?

  • @dirkschannel5817
    @dirkschannel5817 Жыл бұрын

    Great Video Arjan. How would you implement an async decorator for async functions or a decorator that can be used on both sync and async. Still found a solution that "feels" right

  • @spwim
    @spwim9 ай бұрын

    I was trying to accomplish a decorator in a class and couldn't make any sense of it until i saw this video. Arjan explains the best and most generic way of achieving a decorator.

  • @ArjanCodes

    @ArjanCodes

    9 ай бұрын

    Thank you so much!

  • @icoudntfindaname
    @icoudntfindaname6 ай бұрын

    Absolutely great video

  • @ArjanCodes

    @ArjanCodes

    6 ай бұрын

    Thank you so much!

  • @tacozmacleo
    @tacozmacleo Жыл бұрын

    One more fantastic python guide. ❤️ You make it easy to learn. Are you also going to tackle another programming language, like javascript? Or will that hurt your KZread algorithm too much?

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    I expect mixing programming languages will hurt the channel. But I might start another channel in the future that focuses on a different programming language.

  • @ThatOpinionIsWrong
    @ThatOpinionIsWrong Жыл бұрын

    I use decorators mostly for testing and debugging. Having a dedicated decorators for benchmark and logging produces less clutter and I can simply remove the decorator if I no longer need it. I also use my own @NOT_IMPLEMENTED decorator to automatically raise a NotImplemented exception and print message to the console, so I don't have to write the code manually in every function over and over. Otherwise I try to stay away from decorators (unless it's property, dataclass or pytest fixture).

  • @YaroslavOliinyk2023
    @YaroslavOliinyk2023 Жыл бұрын

    Thank you so much for your hard work and the effort you put into your channel. Great video!!! Quick question/ idea for another video Could you describe the difference between Decorator and Proxy Design patterns? Thanks in advance!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Great suggestion!

  • @walkdead94
    @walkdead94 Жыл бұрын

    I usually use the decorators from packages, I didn't had the need to create stuff for myself.. but watching this video, I see there is a lot of small stuff I do a lot in my code that could be an decorator.. just low level stuff like error handling and logging..

  • @thiagoperezsantos
    @thiagoperezsantos Жыл бұрын

    Very good!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you!

  • @JeanDAVID
    @JeanDAVID Жыл бұрын

    powerfully awesome

  • @mansamusa559
    @mansamusa559 Жыл бұрын

    Love it!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks!!

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 Жыл бұрын

    I notice you only talked about function decorators, not class decorators. They have their uses, too. I think the most extensive use I have made of custom decorators so far has been in my DBussy package, a pure-Python wrapper for libdbus. The information defining the D-Bus interface to a Python class is specified via a function decorator on each Python method implementing a D-Bus interface method, with a class decorator to tie it all together. Then when you register your class to receive messages, the dispatch mechanism knows how to decode incoming D-Bus method calls, convert the arguments and dispatch to the appropriate Python method. As a bonus, this same information is used to automatically generate the D-Bus XML introspection format, when another D-Bus peer sends you the introspection message. And conversely, when you introspect another D-Bus peer, this info is used to dynamically generate a “proxy interface class” -- a Python class whose methods simply send the appropriate D-Bus messages and return the responses.

  • @JusticeNDOU
    @JusticeNDOU Жыл бұрын

    The most useful tip with decorators especially in-case where a decorator handles input, for example in authentication the best possible way to handle invalid input is to raise an Error : raise UnAuthenticatedError(message) then handle the error else where on your code, any other means of handling invalid arguments or invalid credentials is bound to cause problems with Flow.

  • @JusticeNDOU

    @JusticeNDOU

    Жыл бұрын

    for example if you handle invalid arguments by passing along a variable or credentials , then if they are invalid and you forget to handle the case where the credentials where invalid in the method or function in which the decorator is being used, you may not find this bug... straight away,

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Yes, good points!

  • @nateriver8261
    @nateriver8261 Жыл бұрын

    Hey, Can you make a video about the difference between decorator and proxy design patterns

  • @xavierboluna1037
    @xavierboluna1037 Жыл бұрын

    OH MY GOD IT FINALLY MAKES SENSE

  • @aryan6536
    @aryan6536 Жыл бұрын

    This is extremely interesting, I am working on a Flask 2.0 project and would like to know if you have any videos on Flask. Search did not find anything apart from GarphQL VS Rest. Thank you.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Haven't done a video on Flask, but soon a video will come out on FastAPI.

  • @aryan6536

    @aryan6536

    Жыл бұрын

    @@ArjanCodes I look forward to it, thank you very much.

  • @boraoku
    @boraoku Жыл бұрын

    Brilliant video. May I ask which IDE is that - that run button and auto switching to terminal is quite nifty.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks! I'm using VS Code.

  • @P4el1co
    @P4el1co Жыл бұрын

    For logging I use loguru - a neat little library that saves me some time

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 Жыл бұрын

    22:38 You shouldn’t have bothered with functools.partial. Just do with_default_logging = with_logging(logger)

  • @adamstrejcovsky8257
    @adamstrejcovsky8257 Жыл бұрын

    He is back!!!

  • @andreaswassmer9516
    @andreaswassmer95168 ай бұрын

    Danke!

  • @ArjanCodes

    @ArjanCodes

    8 ай бұрын

    Happy you enjoyed the video, Andreas!

  • @pablostraub
    @pablostraub Жыл бұрын

    Several years ago the term "aspect oriented programming" was in vogue. Decorators seem to me _the_ way to implement AOP.

  • @noureddineloug2703
    @noureddineloug27035 ай бұрын

    you are gods gift for me thank you very much. are not you planning for doing some projects in python.

  • @quixadhal
    @quixadhal Жыл бұрын

    I've only used a handful of decorators in python, but what I'm curious about is what @abstractmethod brings to the table here, in the first section of the video. I'm familiar with the idea that declaring something abstract (in languages that allow it) prevents you from creating a subclass without providing your own definition of that method. Is that all that's being done here? I admit that not usually being a library programmer, I tend to just document what things need to be done rather than build out a hierarchy to make the compiler enforce it.

  • @demonurge
    @demonurge Жыл бұрын

    Note that if you want decorators to support both sync and async functions, you can switch based on the value of inspect.iscoroutinefunction()

  • @lucianrahl6417
    @lucianrahl6417 Жыл бұрын

    Decorators are a wonderful way to challenge people to explore the more abstract tools in Python and their also wonderful for making DRY-er code. I recently used decorators to support a test structure, where the decorator would display information, e.g. pass/fail the return of all the test, given the test methods only returned bools.

  • @RajNaik-ry9ep
    @RajNaik-ry9ep Жыл бұрын

    Can you explain pyTest specially parameterize pytest where results can be stored and send to end users in proper format

  • @jeju3267
    @jeju3267 Жыл бұрын

    wow real magic

  • @paperexplained
    @paperexplained7 ай бұрын

    I wanted to ask a question but writing my question answered it, thanks

  • @amitneuhaus2989
    @amitneuhaus2989 Жыл бұрын

    Amazingg

  • @michalbotor
    @michalbotor Жыл бұрын

    hey Arjan. can you share your thoughts on using result tuples such as the one below in python? good idea or bad idea? ``` def result(func): def safecall(*args, **kwargs): try: return (None, func(*args, **kwargs)) except Exception as e: return (e, None) return safecall @result def div(x, y): return x / y X = [1, 1, 1, -1] Y = [2, 0, [], 2] for x, y in zip(X, Y): err, res = div(x, y) if err: print(repr(err)) continue print(res) >>> 0.5 >>> ZeroDivisionError('division by zero') >>> TypeError("unsupported operand type(s) for /: 'int' and 'list'") >>> -0.5 ```

  • @Apstergo
    @Apstergo Жыл бұрын

    Lovely vidoe as always here is a video idea for you: How to Code inefficiently.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Haha, good suggestion, with some reverse psychology :)

  • @chevellechris1
    @chevellechris1 Жыл бұрын

    Everyone else is giving proper accolades for the video, so I just came in to say well done for "Pain in the Class" 😂👏

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Haha, thank you Chris!

  • @JPy90
    @JPy90 Жыл бұрын

    Hello! It's possible to use @decorator for wrapping a class method?

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Yes, you can certainly do that. An example where this is used is the @property or @abstractmethod decorators.

  • @orlanino
    @orlanino10 ай бұрын

    I use decorators in fastapi for user's token authorization.

  • @EarlLapus
    @EarlLapus Жыл бұрын

    Is it common/good practice to allow exceptions to be raised inside a wrapper function?

  • @cassidydotdk
    @cassidydotdk5 ай бұрын

    Tak!

  • @ArjanCodes

    @ArjanCodes

    5 ай бұрын

    Thank you for your support, Cassidy!

  • @tanjt107
    @tanjt107 Жыл бұрын

    What is the difference between functools and setting default arguments in function?

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    The difference is that with functools partial you can create different versions of the same function, but with a different value applied, while there’s only one possible default value. A second, more fundamental difference is that partial creates a new function that doesn’t even have the argument anymore.