5 Useful Python Decorators (ft. Carberra)

preview_player
Показать описание
Here are 5 useful decorators that can help make your code more convenient to use! And a big thanks @Carberra for joining in on this video; do check him out if you love Python!

Check out Carberra:

Github repository:

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Learning Python made simple
00:05 Intro
00:32 @retry
02:42 Sponsor
02:52 @cache
05:50 @get_time
07:51 @deprecated
10:58 @on_exit
13:51 Amazing
Рекомендации по теме
Комментарии
Автор

Congratulations on getting the sponsorship deal!

anamoyeee
Автор

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.

wrichik_basu
Автор

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.

finnthirud
Автор

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.

SuperVirus
Автор

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.

AntonioZL
Автор

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.

IceArdor
Автор

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

auroragb
Автор

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.

pldvs
Автор

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

workingguy
Автор

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

juancharlie
Автор

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

dcollett
Автор

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

samrat
Автор

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.

chair
Автор

Love your videos man! Super helpful towards improving my coding practices

texloch
Автор

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 😁

michaelhoffmann
Автор

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

mohamedahmed
Автор

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)

pavfrang
Автор

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?

Kommentierer
Автор

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

brunocunha
Автор

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

deadeyea