5 Useful Python Decorators (ft. Carberra)

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.พ. 2025

ความคิดเห็น • 107

  • @anamoyeee
    @anamoyeee 11 หลายเดือนก่อน +294

    Congratulations on getting the sponsorship deal!

    • @Indently
      @Indently  11 หลายเดือนก่อน +68

      Thanks, they're very easy to work with! Absolutely recommend them :)

    • @DrDeuteron
      @DrDeuteron 10 หลายเดือนก่อน +15

      I’m actually dating them rn. Like right. Now.

    • @DrDeuteron
      @DrDeuteron 10 หลายเดือนก่อน +2

      I just can’t deal with type annotations on an add function. Is this not DEVOlution ? Next we’ll be calling it “addi”, with “addf”, “adds”, “addl”…..I can keep going…addc,

  • @wrichik_basu
    @wrichik_basu 11 หลายเดือนก่อน +66

    Very nice collab! The deprecated decorator was a very much necessary functionality, especially for libraries. Coming from a Java background, not having this decorator in the main Python language surprised me.

    • @sir_no_name1478
      @sir_no_name1478 9 หลายเดือนก่อน +1

      May I ask what you do mean by that?
      Because Java indeed has one.
      You could argue with me that the semantics are not the same but syntactically they are the same ^^.

    • @Nicoder6884
      @Nicoder6884 7 หลายเดือนก่อน +3

      @@sir_no_name1478 He said Python is missing one, not that Java is missing one.

    • @sir_no_name1478
      @sir_no_name1478 7 หลายเดือนก่อน +2

      @@Nicoder6884 oh yeah can't read it seems ^^

  • @AntonioZL
    @AntonioZL 11 หลายเดือนก่อน +40

    One of the best uses of the cache decorator is in recursive functions. Every recursive call get's cached, meaning if you call factorial(n) and then factorial(n+2), only 2 extra recursive calls will be made, for a total of n+2 calls. Any call to factorial(m) for m =< n will also be entirely cached.

    • @urkerab
      @urkerab 11 หลายเดือนก่อน +6

      Fibonacci is the go-to function for demonstrating the cache decorator.

    • @michaelhoffmann2891
      @michaelhoffmann2891 10 หลายเดือนก่อน

      What would be a good demo is some use cases that demonstrate the advantages of @cache vs @lru_cache versus each other. When to use each.

    • @cycrothelargeplanet
      @cycrothelargeplanet 10 หลายเดือนก่อน +1

      If you input numbers like 1000 you get a RecursionError.
      Also isn't it

    • @Omena0
      @Omena0 10 หลายเดือนก่อน

      ​@@cycrothelargeplanetyou can change the limit with the sys module

  • @SuperVirus1978
    @SuperVirus1978 11 หลายเดือนก่อน +52

    When it comes to retries, you may want to have a look at the Tenacity library.
    E.g. Tenacity allows for random wait times, exponential wait times or even only retrying for certain exception types.

    • @Indently
      @Indently  11 หลายเดือนก่อน +11

      Also has a cool name!

    • @kezif
      @kezif 11 หลายเดือนก่อน +1

      yep, certain exception is great. Catching any excepting is just nasty

  • @juancharlie777
    @juancharlie777 11 หลายเดือนก่อน +5

    Simply the best videos for people learning Python. You're a highly effective communicator who has a teaching spirit. Thank you for helping!

    • @Indently
      @Indently  11 หลายเดือนก่อน +1

      Thanks for the kind words :)

  • @bobruddy
    @bobruddy 5 วันที่ผ่านมา

    Thanks!

    • @Indently
      @Indently  5 วันที่ผ่านมา

      Thank you for the support! :)

  • @SalamanderDancer
    @SalamanderDancer 11 หลายเดือนก่อน +8

    Tenacity’s retry decorator is really good and quite expressive.
    Atexit: use a context manager with a try/finally block to close that database for most applications. Atexit mainly applies to long running python programs that are run as a server or daemon.

  • @ego-lay_atman-bay
    @ego-lay_atman-bay 3 หลายเดือนก่อน +1

    I have started using custom decorators to register functions and classes into data computation scripts, like effects on audio files, and specific element parsers for an xml file parser. I feel like decorators are pretty handy for stuff like that.

  • @finnthirud
    @finnthirud 11 หลายเดือนก่อน +9

    atexit: Doug Hellmann's book about the Python 3 standard library pages 995-997 mentions three conditions when atexit will not be invoked: 1) the program dies because of a signal, 2) os._exit() is invoked directly, and 3) a fatal error is detected in the interpreter.

    • @tomwin6975
      @tomwin6975 6 หลายเดือนก่อน +1

      atexit can handle signals, but not all of them; system SIGTERM and SIGINT will be caught by atexit and handled - however, if cleanup handlers are taking too long, OS might terminate the process altogether, so beware of that (Docker does that for example; first it sends SIGTERM, and after 10 seconds, if process is still running, it sends SIGKILL). Not sure about SIGQUIT, but looking at different OS docs, atexit should be able to handle it as well. SIGSTOP cannot be caught or interrupted in most systems, and SIGKILL will forcefully terminate process no matter what in all OS's for all I know. I would have to check the implementation on os._exit(), but if I remember correctly, it also invokes SIGKILL on the process

    • @finnthirud
      @finnthirud 6 หลายเดือนก่อน

      @@tomwin6975 Thank you for the insight.

  • @workingguy3166
    @workingguy3166 9 หลายเดือนก่อน

    Once I finish a project that I'm working on I'll watch a lot of your videos and try to apply some new learnings to my old python projects, really informative things you're pushing out

  • @pldvs
    @pldvs 11 หลายเดือนก่อน +2

    Some code that might be useful to my project, a source that looks well worthy of a sub, and a literal belly laugh. Thanks for that mate.

  • @Sailesh_Bhoite
    @Sailesh_Bhoite 11 หลายเดือนก่อน +4

    I like the sponsor!😂

  • @michaelhoffmann2891
    @michaelhoffmann2891 10 หลายเดือนก่อน

    A fun follow up video would be a brief overview on how to create your own decorators! I've done so, for some client specific code and it's a fun exercise 😁

  • @auroragb
    @auroragb 11 หลายเดือนก่อน +6

    I've also written the retry and the timing decorators a few times for various projects, I wish it were standardized in a built-in module

  • @chair547
    @chair547 11 หลายเดือนก่อน +5

    Probably one of the coolest things I ever created that I unfortunately no longer have was a decorator that would turn a function into a tkinter form automatically.

    • @LF-Me
      @LF-Me 9 หลายเดือนก่อน

      In what way? Like it would create a form for inputting arguments, and displaying function results?

    • @chair547
      @chair547 9 หลายเดือนก่อน

      @@LF-Me yeah exactly

  • @pavfrang
    @pavfrang 8 หลายเดือนก่อน

    Thanks for the great video!
    Obviously you know it, I prefer one-liners for these:
    sum(1 for letter in text if letter in vowels)

  • @brunocunha9043
    @brunocunha9043 11 หลายเดือนก่อน

    Excelent content, thanks for sharing it (:
    For those thinking about counting vowels in a str, the version below has time complexity of O(n)
    ```python
    def count_vowels(input_str: str) -> int:
    vowels = 'aeiouAEIOU'
    return sum(1 for char in input_str if char in vowels)
    ```

  • @samrat00725100
    @samrat00725100 11 หลายเดือนก่อน +3

    You can also use lru_cache instead of cache, it will automatically remove the cache which is not used recently.

  • @patrickbg110
    @patrickbg110 11 หลายเดือนก่อน +1

    Super informative, thank you!

  • @mohamedahmed4461
    @mohamedahmed4461 9 หลายเดือนก่อน

    this was reallly helpfull , i was struggling to write this to evevry function i use to measure time or apply retry functionalliy

  • @dcollett
    @dcollett 11 หลายเดือนก่อน

    These are excellent (as are ALL of your videos). Thank you! 😺

  • @alexanderzikal7244
    @alexanderzikal7244 11 หลายเดือนก่อน

    Very cool stuff, Thank You!

  • @hodiks
    @hodiks 11 หลายเดือนก่อน +1

    It would be helpful if you showed the decorator code in the video as well in video.

  • @crosby3108
    @crosby3108 11 หลายเดือนก่อน

    The sponsor shout out was the best lol 😂😆😂

    • @shreyasj6437
      @shreyasj6437 11 หลายเดือนก่อน

      Absolutely epic😎

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 11 หลายเดือนก่อน

    It's appreciable ❤

  • @sg8nj
    @sg8nj 11 หลายเดือนก่อน

    Amazing🎉

  • @GamersOutcast
    @GamersOutcast 9 หลายเดือนก่อน

    finally a channel that uses a real IDE

  • @jftsang
    @jftsang 11 หลายเดือนก่อน

    Shout-out to the funcy library, while we're here!

  • @Kommentierer
    @Kommentierer 11 หลายเดือนก่อน

    I like to add a filter for exception types when writing a retry decorator. When fetching some stuff, it might fail due to connection issues or a rate limiting. Then it is fine to retry it. But if you parse or transform the result in the same function, it will fail always if something is different than expected, so usually no need to retry it.
    And, my favourite case, if you cause a KeyboardInterrupt at the same time, would it retry it as well?

  • @deadeye1982a
    @deadeye1982a 11 หลายเดือนก่อน

    Instead of own retry decorator, use backoff. It's also compatible with asyncio. But it's an additional dependency.

  • @duchenpaul
    @duchenpaul 11 หลายเดือนก่อน +1

    What is the name of the VS code theme used by Carberra

  • @motomatt5040
    @motomatt5040 5 หลายเดือนก่อน +1

    *Has a video where Carberra pronounces his name*
    still says "car bear uh"
    lol

  • @DerMarkus1982
    @DerMarkus1982 10 หลายเดือนก่อน

    1:37
    17| if retries LT 1 or delay LTE 0:
    18| raise ValueError('Are you high, mate?')
    has me ROFL'ing!

  • @WaxyLT
    @WaxyLT 8 หลายเดือนก่อน

    does anyone know which color scheme carberra was using?

  • @TheBobi321
    @TheBobi321 10 หลายเดือนก่อน

    If the computation of a function is fixed and it will result in the same result everytime, why would I want to use cache and not just store the output in a variable?

    • @Indently
      @Indently  10 หลายเดือนก่อน

      Because what you're talking about is a constant, and creating a constant for every single input scenario is just not ideal, and probably not possible due to the infinite amount of possible inputs.

  • @PR0MINENCE
    @PR0MINENCE 10 หลายเดือนก่อน

    The connect() function does not return, because of the raise. The correct typing should be -> NoReturn

  • @tincustefanlucian7495
    @tincustefanlucian7495 11 หลายเดือนก่อน

    Retry is already done by the requests library. Cache clear was interesting

  • @davidmurphy563
    @davidmurphy563 11 หลายเดือนก่อน +1

    0:49 Did pycharm create the time import for you automatically? Or was it just a jump-cut? That's really convenient if it's pycharm. I moved from pycharm to vscode when I needed the paid for features. Meh, probably vscode will do the same thing if pycharm does and I'll never bother to set it up.

    • @Indently
      @Indently  11 หลายเดือนก่อน +1

      Yeah it was PyCharm :)

    • @rmHawk765
      @rmHawk765 9 หลายเดือนก่อน +1

      You can enable auto imports in VS Code by clicking the curly braces next to Python in the bottom status bar and enabling import completions in the menu that pops up.

    • @davidmurphy563
      @davidmurphy563 9 หลายเดือนก่อน +1

      @@rmHawk765 Huh, it works! Hats off, thanks so much!

  • @__shubham__
    @__shubham__ 9 หลายเดือนก่อน

    What did you use to make "->" appear as actual arrow "→" 11:18 ?

    • @pawelabrams
      @pawelabrams 9 หลายเดือนก่อน +1

      ligatures, fonts in VSCode and PyCharm support them

  • @falklumo
    @falklumo 11 หลายเดือนก่อน +1

    Python decorators are a lot less cool if you have to step into while debugging ... ;) I actually now prefer utility-functions with a lambda, or resources.

  • @spaghettiking653
    @spaghettiking653 11 หลายเดือนก่อน

    These are cool, thanks.
    Although it is my personal opinion that retry and get_time shouldn't be decorators, you could just call the higher-order function whenever you need it instead of affecting the original function.

    • @DrGreenGiant
      @DrGreenGiant 11 หลายเดือนก่อน

      Are the two not equivalent? Except the decorator influences all calls to the wrapped function vs changing the call site online influences one. A decorator is just a higher order function?

    • @spaghettiking653
      @spaghettiking653 11 หลายเดือนก่อน +1

      ​@@DrGreenGiantThat is exactly what I mean, I just think it's better to keep your functions less coupled and just use the "decorator" as a normal function, so e.g. when you want to test your code that runs 3 times, you can work with the original function that runs once and not the version that runs 3 times or whatever.

    • @DrGreenGiant
      @DrGreenGiant 11 หลายเดือนก่อน +1

      @@spaghettiking653 ah I see what you mean, yes. I guess it depends on the use case. I can imagine a worker function that is being sent to a pool, for example, and it would be probably easier to temporarily decorate the worker, than to change the call site. Especially if there is already some higher order stuff going on with partials, for example. But like you say, I can imagine many other cases where it would be much better to wrap the call site rather than the definition. More food for thought, ty!

    • @spaghettiking653
      @spaghettiking653 11 หลายเดือนก่อน +1

      @@DrGreenGiant Ty yourself, I didn't give much thought to threads or any other use cases like that :)

  • @mallninja9805
    @mallninja9805 10 หลายเดือนก่อน +13

    So these aren't 5 Python decorators, they're 5 decorators written by your average Python fan.

    • @AWriterWandering
      @AWriterWandering 9 หลายเดือนก่อน +1

      Cache and atexit are part of the standard library.

  • @МихаилГулев-л9ч
    @МихаилГулев-л9ч 8 หลายเดือนก่อน

    Probably, It will be better to use set() instead string. vowels = set('aeiouAEIOU') and if letter in vowels: ...

  • @Rx512xr
    @Rx512xr 2 หลายเดือนก่อน

    good voice. thx

  • @greedyfishbones
    @greedyfishbones 6 หลายเดือนก่อน

    sponsor made me laugh :)

  • @RS-vu5um
    @RS-vu5um 11 หลายเดือนก่อน

    Can you help me understand how is the @atexit.register functionality different from Context Manager functionality in Python?

    • @D0Samp
      @D0Samp 11 หลายเดือนก่อน

      Context managers are for cleanly disposing resources like files and database connections when they're no longer needed. This even happens when an exception is thrown in a "with" block. atexit is for global cleanup at the end of the program, which is usually not needed with small scripts.

    • @prosodyspeaks4036
      @prosodyspeaks4036 9 หลายเดือนก่อน

      functilonally atexit is placing your whole program in a context manager and running the supplied function in the `finally` block

  • @creativitykabaap9
    @creativitykabaap9 7 หลายเดือนก่อน

    What is the name.of.ide he uses

  • @black-snow
    @black-snow 11 หลายเดือนก่อน +2

    Just using memory is not a memory leak

    • @Indently
      @Indently  11 หลายเดือนก่อน

      I don't remember anyone stating that :)

    • @black-snow
      @black-snow 11 หลายเดือนก่อน

      4:40

    • @jftsang
      @jftsang 11 หลายเดือนก่อน +3

      @cache internally builds a mapping (essentially a dict, but a bit fancier) between the inputs and outputs of each of your function calls. Since this creates a reference to those objects, they don't get garbage collected as long as the function is around, which is typically for the lifetime of the program, long after you're done with them. This can be nasty if you @cache an object method, it prevents the object from being garbage collected at all.

    • @Indently
      @Indently  11 หลายเดือนก่อน

      Good time stamp, but I said: "can lead to memory leaks".

  • @j4s0n39
    @j4s0n39 7 หลายเดือนก่อน +1

    Your retry count is wrong. If you have four retries, there should be five total attempts. The first attempt is not a retry.

  • @dipereira0123
    @dipereira0123 11 หลายเดือนก่อน

    nice =D

  • @conceptsintamil
    @conceptsintamil 11 หลายเดือนก่อน

    I want pip install idently 😊

  • @funnyvideo8677
    @funnyvideo8677 11 หลายเดือนก่อน

    Your s good as it cones

  • @rondamon4408
    @rondamon4408 11 หลายเดือนก่อน

    My favourite is @mark from pytest

    • @nargileh1
      @nargileh1 7 หลายเดือนก่อน +1

      pytest rules, I just love how you can make a fixture out of other fixtures, only need to import the composite fixture and it'll also seamlessly mix up the parametrization of all the fixtures its using under the hood.

  • @yurass1368
    @yurass1368 9 หลายเดือนก่อน

    A good set, but I was a little disappointed because some things were not called by their proper names: cache(memoization). Also surprised that wraps from functools wasn't mentioned.

  • @amankrpandey1
    @amankrpandey1 11 หลายเดือนก่อน +2

    retrying is a python module which is present already

    • @Indently
      @Indently  11 หลายเดือนก่อน

      If you find it remember to share it with the rest of us :)

    • @dreww5866
      @dreww5866 11 หลายเดือนก่อน +1

      Tenacity is a Python package that implements a retry decorator

    • @amankrpandey1
      @amankrpandey1 10 หลายเดือนก่อน

      pip install retrying

    • @amankrpandey1
      @amankrpandey1 10 หลายเดือนก่อน

      Hope it helps ;).. love your content! Learned a lot from it. Keep sharing your knowledge!!

    • @Indently
      @Indently  10 หลายเดือนก่อน

      Ahaha, I didn't know you meant "retrying" as in that was the module name, thanks for sharing!

  • @LambdaCreates
    @LambdaCreates 11 หลายเดือนก่อน +1

    1:37 Are you high, mate?
    this is the kind of stuff I do for side cases in functions
    I give it a casual error 💀

  • @DrDeuteron
    @DrDeuteron 10 หลายเดือนก่อน

    You don’t count the amount of vowels. You count the number of vowels. For some reason this distinction, which is elementary school grammar, has complete collapsed within the last 2 years. Same from less/fewer.

    • @Indently
      @Indently  10 หลายเดือนก่อน +1

      Thanks for the English lesson :)

    • @typeer
      @typeer 9 หลายเดือนก่อน

      You clearly know what he meant

  • @skewty
    @skewty 7 หลายเดือนก่อน

    typical youtuber.. overly simple examples. nobody should be logging with print. now how does your decorator know what logger to use? also make connect async since sync is mostly legacy for such code now

    • @Indently
      @Indently  7 หลายเดือนก่อน

      If you want to show off your knowledge on beginner tutorials on the internet, try including some examples to help other people, otherwise university might be a better environment for your critique :)

  • @atommax_1676
    @atommax_1676 11 หลายเดือนก่อน

    Wow, deprecated and atexit.register ones are really useful
    Gonna use them in my project, thank you❤

  • @drdca8263
    @drdca8263 11 หลายเดือนก่อน +2

    2:43 : wait, didn’t you say at the start of the video that it was sponsored by indently? Then why do you say it isn’t sponsored?🩳

    • @sims234ify
      @sims234ify 4 หลายเดือนก่อน

      woosh