What are Decorators in Python? 2MinutesPy

  Рет қаралды 24,350

2MinutesPy

2MinutesPy

Күн бұрын

Пікірлер: 53
@Hoxle-87
@Hoxle-87 11 ай бұрын
This is better than gold! Clearly explained. No BS.
@2MinutesPy
@2MinutesPy 11 ай бұрын
Thanks!
@maxpythoneer
@maxpythoneer 11 ай бұрын
I've worked with decorators and used them in my Python programs, there are several built-in decorators also that can ease your work. Great one...
@2MinutesPy
@2MinutesPy 11 ай бұрын
Good to know!
@Darkev77
@Darkev77 11 ай бұрын
Beautiful and elegant
@2MinutesPy
@2MinutesPy 11 ай бұрын
Thank you! Cheers!
@sveneFX
@sveneFX 6 ай бұрын
This channel is just great! No talking around, just clear and understandable knowledge in just 2 minutes. Thank you!
@2MinutesPy
@2MinutesPy 6 ай бұрын
Wow, thanks!
@AcessHall
@AcessHall 11 ай бұрын
Pure gold
@2MinutesPy
@2MinutesPy 11 ай бұрын
Thanks for appreciation.
@SunsetofMana
@SunsetofMana 11 ай бұрын
Another amazingly simple, yet clear, and most impressively - concise explanation of a technical topic from this channel. Thank you and please keep making more!
@2MinutesPy
@2MinutesPy 11 ай бұрын
Glad you liked it!
@juanmanuelmillansanchez8165
@juanmanuelmillansanchez8165 10 ай бұрын
I remember asking for this topic. Just wanted to thank you, got it perfectly!
@2MinutesPy
@2MinutesPy 10 ай бұрын
got u bruh
@skbabasaba
@skbabasaba 11 ай бұрын
Great job. Keep producing the great stuff.
@2MinutesPy
@2MinutesPy 11 ай бұрын
Thanks, will do!
@sudhritiswaransinha9875
@sudhritiswaransinha9875 7 ай бұрын
Please make a video on Concurrency: How do you achieve concurrency in Python? Please your explanation is so good !
@2MinutesPy
@2MinutesPy 7 ай бұрын
definitely
@robertodelgado6387
@robertodelgado6387 6 ай бұрын
Wonderfully explained, well done my friend!
@2MinutesPy
@2MinutesPy 6 ай бұрын
Thank you kindly!
@abdalla4425
@abdalla4425 11 ай бұрын
So much easier than the docs for beginners!!
@2MinutesPy
@2MinutesPy 11 ай бұрын
Great
@dweepverma3662
@dweepverma3662 11 ай бұрын
Well explained... Keep it up
@2MinutesPy
@2MinutesPy 11 ай бұрын
Thanks a lot
@cyberlando
@cyberlando 11 ай бұрын
I saw your response of someone asking why do you need the wrapper function. I still don’t understand why you couldn’t just place everything in the wrapper function just inside the scope of log_function(func) and return func()
@2MinutesPy
@2MinutesPy 11 ай бұрын
What you're tryin to say is why can't we place the code directly within the log_function. Okay let me explain According to you: def log_function(func): print(f"Calling function {func.__name__}") return func() @log_function def say_hello(): return "Hello, world!" This approach is okay but in limited cases, here log_function immediately calls the func() after printing the logging message. Now in this approach, you can't call the say_hello function as this will result in an error. print(say_hello()) # error print(say_hello) # code will execute There's no flexibility in this code, you can't do much. But but but... What if your function accepts some argument, how will you pass that argument? You can't pass it in the log_function, you need a nested function to handle all this. That's where a funtion like wrapper needed to preserve the signature of the original function and simultanoeusly modifying the behaviour. For instance, take a look at the following example: def log_function(func): def wrapper(*args): print(f"Calling function {func.__name__}") return func(*args) return wrapper @log_function def say_hello(text): return text print(say_hello("Hello")) Here, wrapper function handles all the stuff printing logging msg and returning the func with argument. Now when you called the say_hello function with an argument, it is passed to the log_function and log_function hand over to wrapper function to do the stuff. After all the the modification, the log_function returns the modified function.
@2MinutesPy
@2MinutesPy 11 ай бұрын
I must say, that is a great question. Appreciate it.
@dweepverma3662
@dweepverma3662 11 ай бұрын
The same question I thought in my mind, but got the answer here.
@dweepverma3662
@dweepverma3662 11 ай бұрын
​@@2MinutesPythanks for explaining this
@FactFinders-of3mu
@FactFinders-of3mu 6 ай бұрын
I have no word to thank keep it up
@2MinutesPy
@2MinutesPy 6 ай бұрын
Yeah, thanks for the support
@MuhammadShafi-c8n
@MuhammadShafi-c8n 3 ай бұрын
really a good explanation every second worth
@2MinutesPy
@2MinutesPy 3 ай бұрын
Glad you liked it
@rryann088
@rryann088 5 ай бұрын
cool Thanks! beautifully explained
@2MinutesPy
@2MinutesPy 5 ай бұрын
You're welcome!
@ArdakHere
@ArdakHere 5 ай бұрын
Very clean, thanks!
@2MinutesPy
@2MinutesPy 5 ай бұрын
Glad you like it!
@Rahul5556e
@Rahul5556e 11 ай бұрын
I needed that one❤
@emilmemmedsoy770
@emilmemmedsoy770 10 ай бұрын
Great explanation. Worth it more views
@2MinutesPy
@2MinutesPy 10 ай бұрын
Glad you think so!
@DiscordHomeOffice
@DiscordHomeOffice 11 ай бұрын
Why do I need a wrapper in my decorater function? What is the principle behind the wrapper?
@2MinutesPy
@2MinutesPy 11 ай бұрын
The wrapper calls the original function and can modify the behaviour in some way which allows the decorator to extend the functionality of the original function without changing it's code directly. When you apply decorate a function, for instance, @log_function def say_hello(): The above code is equivalent to say_hello = log_function(say_hello) Now this say_hello name points to the wrapper function within log_function and if you print (print(say_hello), you'll get the wrapper function reference, which means the wrapper function is needed to preserve the original function's signature while adding the extra behaviour or functionality.
@krzysiekkrzysiek9059
@krzysiekkrzysiek9059 9 ай бұрын
Well explained.
@2MinutesPy
@2MinutesPy 9 ай бұрын
Thanks
@emasa97
@emasa97 4 ай бұрын
hey shouldn't the numbers be 16 and 50? it looks like the order of the decorators were the other way around :/
@2MinutesPy
@2MinutesPy 4 ай бұрын
And could you explain how
@emasa97
@emasa97 4 ай бұрын
​@@2MinutesPy First sum 2+2=4, then square 4^2=16
@emasa97
@emasa97 4 ай бұрын
Okay now I see the order is nested like 2:21. What you say at 2:08 was confusing for me
@2MinutesPy
@2MinutesPy 4 ай бұрын
@@emasa97 for the first case, 2 will be squared first, we get 4 and the it will get summed 4+4=8 for the second case, 5 will get summed, we get 10 and then it will be squared 10**2=100 Hence, we got 8 and 100 in the video.
@sllakshancc
@sllakshancc 6 ай бұрын
cool
@2MinutesPy
@2MinutesPy 6 ай бұрын
👍
@kwameadoko9542
@kwameadoko9542 8 ай бұрын
Let’s say a better way of writing helpers.
@DashingData66666
@DashingData66666 5 ай бұрын
This all has been copy pasted from the chat gpt result great
How to use list comprehension in Python | 2MinutesPy
2:16
2MinutesPy
Рет қаралды 9 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 460 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
the 7 dwarfs test demo 1
15:20
R. P
Рет қаралды 1
Python Generators
15:32
mCoding
Рет қаралды 146 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 112 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 129 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 916 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 345 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 414 М.
What Exactly are "Context Managers" in Python?
2:51
2MinutesPy
Рет қаралды 5 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41