Тёмный
No video :(

What Are Python DECORATORS??  

b001
Подписаться 258 тыс.
Просмотров 653 тыс.
50% 1

Опубликовано:

 

23 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 226   
@rodanban222
@rodanban222 Год назад
the most important thing is that decorators should cover possible arguments to function def wrapper(*args, **kwargs): func(*args, **kwargs)
@MelihGMC2
@MelihGMC2 5 месяцев назад
649 likes and 0 replies? Let me fix that
@rodanban222
@rodanban222 5 месяцев назад
@@MelihGMC2 you're the best :)
@willd0g
@willd0g 3 месяца назад
Definitely preferred the simplest case first - Previous examples have always tripped me up. Thanks for the comment now that ive got the basics
@30sachinsharma4
@30sachinsharma4 2 месяца назад
Yup bro for arguments
@KousikMalladi
@KousikMalladi Месяц назад
Hey man, could you tell me something like, where to learn python from
@James-the-elder
@James-the-elder Год назад
This is the most straightforward explanation of Python decorators I've seen so far.
@TAP7a
@TAP7a Год назад
Absolutely. Having read dozens of articles, this 60s video is the only presentations I've seen that actually gets the message across
@bankaihampter2802
@bankaihampter2802 9 месяцев назад
But it barely scratches a surface. Decorators can take also args and kwargs, there are functools for keeping a function name and signature the same. Overall decorators are much harder than this
@James-the-elder
@James-the-elder 9 месяцев назад
@@bankaihampter2802Yes but it's a 60 seconds video. If you understand the basics, you are more likely to read further
@charlescrawford9972
@charlescrawford9972 9 месяцев назад
I think I'm missing something fundamental here. When should a decorator be utilized?
@mozvi1436
@mozvi1436 9 месяцев назад
This is the best beginner explanation imo yep
@2Sor2Fig
@2Sor2Fig 2 месяца назад
Decorators are one of the many beautiful layers of Python. My favorite practical usage was a decorator that allows me to keep a database connection open and automatically rollback in case of an error.
@0xdani000
@0xdani000 25 дней назад
This sounds great, how did a decorator help you with this? I find it hard to see when decorators are necessary
@hudsonreynolds4349
@hudsonreynolds4349 10 дней назад
​@@0xdani000you would call the function with the decorator in some way to establish the database connection outside of the function to keeo the connection if the function has a problem
@twujstary1129
@twujstary1129 4 дня назад
wouldnt a context manager be better suited for this scenario? tbh idk, just askin
@richi1235
@richi1235 4 дня назад
​@@0xdani000never neccessary
@SpaghettDev
@SpaghettDev Год назад
to make it more clear, a decorated function called f basically does this: f = my_decorator(f)
@ferrumignis
@ferrumignis Год назад
Why is the wrapper function within the decorater function needed?
@SpaghettDev
@SpaghettDev Год назад
@@ferrumignis because if there is no wrapper function within the decorator, then the function f will get asigned to the result of f, which will make f no longer a callable function. Basically, if no wrapper is within the decorator f, f = my_decorator(f) will turn into f = f()
@charlescrawford9972
@charlescrawford9972 9 месяцев назад
@@SpaghettDev So it's for localization of functions? I've gone over a lot of tutorials but this is the most confusing concept to me so far.
@lordmahakaal
@lordmahakaal 3 месяца назад
​@@SpaghettDevcan you tell where in program behind the scene this line f=my_decorator(f) happens. I am still confused. Also what about when do_this() is called? Thanks
@span4ev
@span4ev 4 месяца назад
I just needed a simple example of how decorators work to get the function name for error logging. Thanks
@murphygreen8484
@murphygreen8484 2 месяца назад
From functools import wraps
@MichaelProstka
@MichaelProstka Год назад
Kudos! This made WAY more sense than the 30 min video i just watched 😂
@FundamSrijan
@FundamSrijan 5 месяцев назад
Freaking true
@irfanyaqub9643
@irfanyaqub9643 29 дней назад
But as a java developer, i dont see the use of it anyway, You still have to call the function,
@ugulbertosanchez7978
@ugulbertosanchez7978 Год назад
FINALLY UNDERSTOOD IT!!!! thank you 😃
@xPlay5r
@xPlay5r Месяц назад
Also ChatGPT: class MyClass: def __init__(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2 def existing_function(self): # Logic of existing function result = self.arg1 + self.arg2 print(f"Result: {result}") return result def my_function(self, custom_class): # Using custom_class into my_function result = custom_class.existing_function() # Additional actions print("Additional actions") return result obj1 = MyClass(10, 20) output = obj1.my_function(obj1)
@alisoltani5636
@alisoltani5636 3 месяца назад
I have learned something that i used to do manually Thanks
@josiah_maddux
@josiah_maddux 8 месяцев назад
Solid work. I was just wondering about these, lol
@angelsv
@angelsv 9 месяцев назад
Decorators? To me, sounds like a nice Christmas gift with a lot of fancy "decorators" lol
@user-bn2nt4dn9j
@user-bn2nt4dn9j 5 дней назад
Best and easiest explanation
@angeloc700
@angeloc700 3 месяца назад
I first learned decorators when writing Ruby code a long time ago and still don't like them because they make code less readable. But, like ternary operators, we all still use them 😂
@leesixtwooneone
@leesixtwooneone 6 месяцев назад
Jeez. Decorator had been just a separator between cool code and just code for me. Thank you for letting me know what decorator is and the usefulness of decorator.
@pyromancerforhire
@pyromancerforhire 3 месяца назад
That's very functional for the imperativetiness of Python
@sunayraj
@sunayraj 11 месяцев назад
Man, one can easily drag this into 30 mins *demy class.. Thank you.
@anthonymann7679
@anthonymann7679 Год назад
What's a real world example as to why you would do this as opposed to just calling the functions normally?
@infiniteplanes5775
@infiniteplanes5775 Год назад
If you want to add special functionality to certain functions. This would be particularly useful for multiple functions or if you’re developing a tool for others to use
@RQ321
@RQ321 Год назад
Well… I made a tool once that for 3DSMax a couple years back. Some functions required a modifier to be added to a mesh, some didn’t. I used a decorator on those functions to then throw an assert, notifying the user that the modifier needed to be added to the mesh. This allows you to check for requirements on certain functions without actually including the checking code in the function itself. Allowing me to reuse the check code for multiple functions instead… Arguably, you can create checking functions that can be called in other functions that perform the same task. Allowing the same reuse of code. However, I like using it because at a glance, I can see what requirements a function has based on it’s decorators.
@TheBomb420
@TheBomb420 Год назад
Discord bots. Every single one.
@goncaloazevedo9822
@goncaloazevedo9822 9 месяцев назад
It's called a decorator because what it basically does is the decorator design pattern. You can use this to time a function call, or, a very good use case for this is also memoization/caching. Any decorator pattern will very likely make use of this
@Blaisem
@Blaisem 8 месяцев назад
It sounds like any time you need a wrapper function spanning multiple functions. The video mentions measuring the function time for example. You could create a measuring function as a decorator and apply that to any function whose time you want to measure. I'm also just learning about this, but I think in most of these cases I would have just refactored an extra argument to the functions to pass in an object (variable or class) with the wrapper method or the results of the wrapper method, depending on what it needed. I can see the decorator simplifying refactoring, because you can just create a standalone function somewhere and then add @ everywhere, rather than interfering with the definition of the functions, but on the other hand, that seems like a recipe for long-term headaches in code maintenance. If there are different decorators littering the code, your brain has to go a lot further to tie the logic of it all together.
@Cloudy_perpetrator00
@Cloudy_perpetrator00 26 дней назад
With the help of time module, it would make a great 'processing data' on some programs.
@venky1922
@venky1922 Год назад
Thanks bro big shout out❤❤.It was great..
@SomethingRandomChannel
@SomethingRandomChannel 7 месяцев назад
Dude thank you this is great thanks
@curiousmind4870
@curiousmind4870 4 месяца назад
Beautiful!
@octillionbees5516
@octillionbees5516 9 месяцев назад
This is actually a monad!! It’s super cool that python has explicit support for them!
@Steadyrock100
@Steadyrock100 8 месяцев назад
I'm learning new coding things everyday! You got some info on dictionaries? That would help!! You got a subscriber!!!!!!!!!!
@bloxfruit_player788
@bloxfruit_player788 Год назад
Me after 3000000 tutorials of decorators abt how they work, this video👿:
@timesink1024
@timesink1024 8 месяцев назад
lmao same thing with pointers in C. so overhyped and over/underexplained when you finally get a good explanation you're like "really?"
@-SHEESH-YIKES-
@-SHEESH-YIKES- 7 месяцев назад
I hope 🤞 one day I’d be able to code 🧑‍💻 like you bro 😎
@prakashhariharabalan621
@prakashhariharabalan621 7 месяцев назад
Function pointers from c/c++
@epsi
@epsi Год назад
Judging by the comments, this syntax seems like wizardry to many people. Decorator syntax (@decorator) simply takes the function you are decorating and returns a wrapped function with the same name: def cache(f): results = dict() def wrapper(n): key_missing = 'cache key not found' if (r := results.get(n, key_missing)) == key_missing: results[n] = f(n) else: print(f'using cached result {{{n}: {r}}}') return results[n] return wrapper # @cache # def plus1(n): # return n+1 def plus1(n): return n+1 plus1 = cache(plus1) print(plus1(2)) print(plus1(1)) print(plus1(0)) print(plus1(2)) # Output: # 3 # 2 # 1 # using cached result {2: 3} # 3 That's the power of decorators. You can see a more generalized variant of this caching/memoizing idea in functools.cache and even one that only keeps a limited number of values in functools.lru_cache.
@aislancesar
@aislancesar Год назад
Print debugging just evolved...
@KaviBisarya
@KaviBisarya Год назад
You are amazing at this!
@AOLARTES
@AOLARTES Год назад
Ty!
@BiteSizeP0dcasts
@BiteSizeP0dcasts 8 месяцев назад
That just looks like functions but with extra steps
@RonenCruze-ci3vw
@RonenCruze-ci3vw 5 месяцев назад
You don't see that as cleaner code?
@irfankhanpatan4415
@irfankhanpatan4415 Месяц назад
Well explained
@ElantraMan
@ElantraMan 8 месяцев назад
*laughs in closures*
@TaylorPeterson913
@TaylorPeterson913 Год назад
I needed to know this!
@JustaJabberwocky
@JustaJabberwocky Год назад
Thx man
@jorenmartijn
@jorenmartijn 2 месяца назад
Sounds a lot like a class with a constructor
@AverageSensei
@AverageSensei 4 месяца назад
I know how to do print("Hello World!") can I join the club?
@BigstickNick
@BigstickNick 29 дней назад
I’m so bad at this…I’m relatively new(8 weeks). But I’m just not grasping it
@ObiwanNekody
@ObiwanNekody 11 месяцев назад
Awesome is what they are.
@ohimdabiggestbird
@ohimdabiggestbird Год назад
a great video as always, i have a suggestion that would probably take long normal videos, or multiple #short videos, but it would be appreciated by beginners like me... so long story short i wanted to make a snake game with pygame, but all tutorials contained "classes" and stuff related to classes, but i have never understood classes, as i found them very confusing plus not really useful in any way, as i have made multiple projects not really needing to use classes, and i didn't really want to just copy/paste the code from the tutorial, i wanted to actually understand what i was doing, what im saying is we would be very thankful if you could do tutorials explaining classes, what they are, how to use them, and most importantly why to use them, in a simple way for us beginners to comprehend, we'll be so grateful if you could fulfill my request, and from the bottom of my heart i thank you for all the effort you put into your content, keep up the great work (if you could, reply to my comment telling me if you would make or not to make the videos i've suggested)
@b001
@b001 Год назад
Thank you for the sincere comments! Yes, I will absolutely be making videos on classes, it’s been on the agenda for a while now, just haven’t found the time yet, but it’s coming soon!
@patrickbyers3723
@patrickbyers3723 Год назад
Great simple explanation!
@OoverHeaven
@OoverHeaven 2 месяца назад
What would be the typical use case of a decorator? Solely for logging?
@NamChin846
@NamChin846 5 месяцев назад
I like your vids but bro how do you "auto fill" the code? did u do it backwards and press ctrl z all the time? how else do you autofill the code in the exact right spots etc?
@acecabezon
@acecabezon 17 дней назад
Great explanation. I still hate decorators (super ugly), but at least I understand them better.
@harleyfioretti1484
@harleyfioretti1484 3 месяца назад
I thought he was gonna rap about python and was excited. Still good video😅
@valeriodeluca3391
@valeriodeluca3391 8 месяцев назад
Thanks 🙂 Decorators can "decorate" only other functions or other python elements too?
@frenches1995
@frenches1995 8 месяцев назад
ChatGPT was suggesting to use decorators in my code and I've never heard of it at that point! So my answer was like I know christmas is around the corner but stop hallucinating about decorating my code😂😂
@bigjamar
@bigjamar Год назад
excelente..muchas gracias!
@ModifiedMike
@ModifiedMike 4 дня назад
I JUST STARTED BUT WHY IS THIS SO CONFUSING FOR MY BRAIN TO UNDERSTAND!
@bassturtle4
@bassturtle4 Год назад
This is amazing, keep it up
@JM_Tushe
@JM_Tushe Год назад
Nice example, I know what it does, I don't know what it can be used to.
@jesterthelegend926
@jesterthelegend926 3 месяца назад
new to programming. Know the basics... Made a discord bot in python and had to use some "decorators" for the first time. Didn't know what they were called until know or how they worked.
@user-ip1rx2vs6j
@user-ip1rx2vs6j 8 месяцев назад
Looks like a callback function in javascript to me.
@Luix
@Luix 9 месяцев назад
Brain explosion
@raviachan3071
@raviachan3071 Год назад
What IDE and theme is this? Looks great
@forstuffwow7145
@forstuffwow7145 Год назад
read thetop part of video
@shami9773
@shami9773 5 месяцев назад
I am very very new to python and was wondering how you skip to the next line without executing the function.
@Olaf2745
@Olaf2745 Год назад
What is the difference between the decorater and the wrapper. Or rather what is the use of the wrapper, doesn’t the decorator do it’s job?
@BanAaron
@BanAaron Год назад
A decorater is a wrapper. They're the same thing.
@Ruchunteur
@Ruchunteur Год назад
the "wrapper" function could have been called anything you want. It doesn't have to be call wrapper. as long as it is return at the end it's all good. When he call "do_this()" it is as if he try to execute the function that is return by "my_decorator" . So yeah, the decorator allow you to add some additional logic to a function call. it's wrap itself around your function if your will
@Spyxxer
@Spyxxer 8 месяцев назад
Looks redundant to me that innermost function especially just calling it direct looks cleaner but i feel there must be a reason for the wrapper
@marino3034
@marino3034 5 месяцев назад
Can you show the input output caches?
@RoyalYoutube_PRO
@RoyalYoutube_PRO 4 месяца назад
What is the relevance of wrapper?
@theupsider
@theupsider 5 месяцев назад
Funny how decorators are simply the implementation of the decorator pattern.
@florind7056
@florind7056 7 месяцев назад
decorators are like HOC in react
@plazmaplayz2499
@plazmaplayz2499 Год назад
why is the purpose of def wrapper (): func?
@ojasbhalerao3448
@ojasbhalerao3448 Месяц назад
Is this similar to wrapAsync func in javascript?
@SobTim-eu3xu
@SobTim-eu3xu 2 месяца назад
Tnx
@_danfiz
@_danfiz Год назад
So calling the functions below wont do anything? It will only run the decorator? Or even we put @my_decorator it is still necessary to call those two functions?
@b001
@b001 Год назад
Yes, it’s still necessary to call the function. When the function is called, it checks if it has a decorator on top before executing, if it does, Python passes the called function into the my_decorator function, and essentially gets replaced by the wrapper function which has extra functionality “wrapped” around the original function
@Joso997
@Joso997 Год назад
​@@b001it sounds like a delegate when you do not want to use classes
@sweethomes674
@sweethomes674 Год назад
Excellent
@MonirulIslam-ud3px
@MonirulIslam-ud3px 2 месяца назад
Whats the font name and theme name?
@warido37
@warido37 Год назад
do this. do that.
@wissemaljazairi
@wissemaljazairi Год назад
Is it possible to combine multiple decorators?
@aloobaloo68
@aloobaloo68 Год назад
what's the theme of your editor? i'm asking for the third time 😂
@piedepew
@piedepew Год назад
Who asked?😂 /S he be like
@soubhagyasadhukhan4531
@soubhagyasadhukhan4531 Год назад
VS code most probably
@lokeshkumar-dq7yl
@lokeshkumar-dq7yl 11 месяцев назад
Spyder
@SOme_rAnDOm_GuY908
@SOme_rAnDOm_GuY908 11 месяцев назад
shades of purple Edit: Them name :)
@maestrulgamer9695
@maestrulgamer9695 5 месяцев назад
What kind of program would need this?
@adithyag
@adithyag 7 месяцев назад
what is the theme you use for ur visual studio code?
@unvein1863
@unvein1863 6 месяцев назад
this is pycharm and he's using retrowave
@kstelakis
@kstelakis 7 месяцев назад
could this be used to create monads?
@irfanyaqub9643
@irfanyaqub9643 29 дней назад
But as a java developer i dont see the use of it, You still have to call the functions themself then why decorator? there must must be more info to this that you have not explain.
@j.r.r.tolkien8724
@j.r.r.tolkien8724 3 месяца назад
Like closures in JavaScript I suppose.
@andreiinthedesktopworld1178
Software pls
@TheRuancarlo
@TheRuancarlo Год назад
LOL, can I create a decorator as a class method and use it inside another class?
@LastSpark
@LastSpark 2 месяца назад
what is that color scheme
@user1lyy
@user1lyy 17 дней назад
Theme name ?
@Jirayu.Kaewprateep
@Jirayu.Kaewprateep 2 месяца назад
🥺💬 I think it is utility to help interface programmer to access of target function with a sequence logics. 📺💬📺💬📺💬 Give me some examples ... 🧸💬 You cannot have AgentID with callID and you cannot register because they return AgentID. 📺💬 Do you mean you cannot create conference call from existing callID ? 📺💬 Next time do not forget wrote the function 🐑💬 ➰ Hmmmmm.... *** Do not miss understand me, I am not looking from someone mistake and I use it in creative way ***
@sudeepranganath5975
@sudeepranganath5975 6 месяцев назад
AOP in spring. Simple here.
@mistermister1541
@mistermister1541 8 месяцев назад
Python is about clever code over simple readable code.
@olivers.7821
@olivers.7821 9 месяцев назад
How do all of these just instantly appear?
@UvinduChamath01
@UvinduChamath01 Месяц назад
Bro what is this theme?
@aliakbarkazeminiya2758
@aliakbarkazeminiya2758 Месяц назад
what this is theme ?
@raurok
@raurok Год назад
Why do you have to return the wrapper ?
@b001
@b001 Год назад
It’s essentially the modified version of the function you pass in. For example, do_this gets passed into my_decorator, and the wrapper function is returned to take it’s place
@mustafagamer7358
@mustafagamer7358 Год назад
​@@b001why don't u just execute the do_this function in decrator instead of the wrapper or just make return the fun()
@uclevan7286
@uclevan7286 28 дней назад
nice
@Adinasa2
@Adinasa2 6 месяцев назад
Which theme
@winter7885
@winter7885 8 месяцев назад
why dont we just do my_decorator(do_this): is there any diffeerence to it
@rodbrowning
@rodbrowning Год назад
What If the function has arguments?
@lukaswhynot3386
@lukaswhynot3386 Год назад
what is the font?
@seanyamamoto8757
@seanyamamoto8757 Год назад
Synthwave 84
@kidaz
@kidaz 10 месяцев назад
Why do you have to define wrapper?
@goncaloazevedo9822
@goncaloazevedo9822 9 месяцев назад
Because in reality you are calling wrapper when you call your function
@SethRadius1
@SethRadius1 8 месяцев назад
Me right now on Day Zero (preemptively) the trying to learn Python and attempting to understand this video: @_@;;
@montry.
@montry. 5 месяцев назад
So middleware?
@polycrystallinecandy
@polycrystallinecandy 8 месяцев назад
Poor man's monad
@stark8164
@stark8164 Год назад
How are you pasting things like that?
@JustATempest
@JustATempest Год назад
He's control shift z. It's redo.
@stat_life
@stat_life Год назад
I still didn't get it
@UCKszbcV
@UCKszbcV Год назад
So...decorators are basically a function factory?
@theseasmaroulis5331
@theseasmaroulis5331 Год назад
not exactly, it is syntactic sugar for the decorator design pattern.
@ashersilver7388
@ashersilver7388 Год назад
Is it possible to pass decorators around like modules?
@fffmpeg
@fffmpeg Год назад
yes, just import like anything
@midvisexual6710
@midvisexual6710 7 месяцев назад
Bro, I swear I can't watch a python tip video without remembering that pythonistrash guy from instagram. Now everytime I watch any other python short or reel I keep thinking of "BIM BIM BAM BAM" 😰
Далее
5 Good Python Habits
17:35
Просмотров 500 тыс.
Python Decorators in 15 Minutes
15:14
Просмотров 437 тыс.
#JasonStatham being iconic
00:38
Просмотров 274 тыс.
Мама приболела😂@kak__oska
00:16
Просмотров 804 тыс.
The Algorithm Behind Spell Checkers
13:02
Просмотров 411 тыс.
Python 101: Learn the 5 Must-Know Concepts
20:00
Просмотров 1,1 млн
How To Create Decorator Functions In Python
4:59
Просмотров 6 тыс.
5 Useful F-String Tricks In Python
10:02
Просмотров 297 тыс.
PLEASE Use These 5 Python Decorators
20:12
Просмотров 108 тыс.
*Args and **Kwargs in Python
3:49
Просмотров 266 тыс.
3 PYTHON AUTOMATION PROJECTS FOR BEGINNERS
17:00
Просмотров 1,6 млн
Please Master These 10 Python Functions…
22:17
Просмотров 137 тыс.