Decorators in Python: How to Write Your Own Custom Decorators

preview_player
Показать описание
The decorator design pattern in software engineering, is generally when you extend something (like an object or a function) with additional functionality.

In this video, you'll learn what a decorator is, and learn how to apply them to time and optimize a function.

00:00 Introduction
01:09 What are decorators?
01:37 Writing a "timer" decorator
04:51 Using a decorator to cache Fibonacci
08:50 Python built-in decorators

#python #decorators
Рекомендации по теме
Комментарии
Автор

This is the best video about python decorators i've ever seen! They examples are very helpful and simple.

michaeldprovenzano
Автор

Just wanted to let you know your channel actually helped get me a SWE internship, so thanks a lot!

Also another example of a built-in decorator is the cache decorator from the functools module which actually would probably work on the fibonacci function example used here.

TopSecretBase
Автор

Thanks for the video. I also like the VS code theme and font. Does someone know the name of it?

chrisk
Автор

I playing with the fibonacci algorithm and saw that if there's a cache_result on the main fibonacci not the one with the timer it's incredibly fast! It does the 496th number in less than a sec; if done propperly!

moony-
Автор

from functools import lru_cache

@lru_cache(maxsize=None)
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

# First function call will be slow as it computes and caches the results.
print(fibonacci(10))

# Subsequent calls with the same argument will be faster due to caching.
print(fibonacci(10))

nesa
Автор

Greetings, @pixegami the content that you share is of great value, I am very grateful to you. For when you plan to make another SAAS

jesusperdomo
Автор

Use time.perf_counter() not time.time()

sir_brian_d