Python Decorators 1: The Basics

  Рет қаралды 40,588

Sebastiaan Mathôt

Sebastiaan Mathôt

Күн бұрын

Пікірлер: 108
@appophiss3890
@appophiss3890 5 жыл бұрын
This is the most underrated video/channel I've ever seen
@fuwadhasan
@fuwadhasan 3 жыл бұрын
Agree
@vaani6393
@vaani6393 4 жыл бұрын
You came like a saviour in my search options
@Tptkk
@Tptkk 6 жыл бұрын
I'm blown away by your videos! So much information and on top of that presented in a digestible way. Very unique. You got talent! Thanks! Keep up the great work! Greetings from Germany
@macacinelecanale
@macacinelecanale 2 жыл бұрын
The clearest explanation of decorators video that I found, and watched a lot of them! You have very good teaching skills!
@rajburtonpatel6702
@rajburtonpatel6702 Жыл бұрын
I love the way you write list comprehensions from the inside to out. Really helps get them right and explain them well.
@Naton
@Naton 4 жыл бұрын
I love the pace. Followed without pausing
@andreashohmann957
@andreashohmann957 3 жыл бұрын
Best explanation i have seen so far!!!
@programaticamentefalando2918
@programaticamentefalando2918 3 жыл бұрын
When the explanation is hard to understand there is bitty chance the conveyor of the information does not have much knowledge about the topic himself. Which, is not the case here. Agreed.
@nareshgb1
@nareshgb1 5 жыл бұрын
Flat out the best video on decorators - a REAL, USEFUL example. Awesome.
@JeremyFisher
@JeremyFisher 4 жыл бұрын
Good Video, yes useful ? no for word in names: print(camelcase(word))
@satoshinakamoto171
@satoshinakamoto171 4 жыл бұрын
probably the best tutorial on decorators on yt. deserves more views
@brucewu1843
@brucewu1843 4 жыл бұрын
Wow this guy exactly knows what the audience wants. 10/10. good job man!
@antonyinjila7152
@antonyinjila7152 3 жыл бұрын
This guy;s teaching style is on another level. Thank you for the time and effort .
@stevenwilson5556
@stevenwilson5556 2 жыл бұрын
well said. I hope you do *many* move videos. You should consider teaching this stuff if you don't already.
@thebuggser2752
@thebuggser2752 3 жыл бұрын
What a great presentation! I’ve been looking for a good explanation of what a decorator is, and this nailed it! A home run! Thanks!
@stevenwilson5556
@stevenwilson5556 2 жыл бұрын
Agree with this.
@joebater7830
@joebater7830 4 жыл бұрын
The first three minutes of this explained what I've been struggling with from multiple books and other videos! bravo!
@sheikhakbar2067
@sheikhakbar2067 4 жыл бұрын
Thanks for the example; it illustrated the concept of decorators perfectly!
@aaronbaldwin2845
@aaronbaldwin2845 3 жыл бұрын
You are such a great conveyer of information. Very concise and understandable. I appreciate these videos
@francescodiniccolo
@francescodiniccolo 6 жыл бұрын
I finally properly understood what a decorator is. Thank you!
@stevenwilson5556
@stevenwilson5556 2 жыл бұрын
Brilliant video, well explained.. was able to duplicate what you did and I am still a raw beginner only been trying to learn Python for about a week give or take. Thank you.
2 жыл бұрын
Glad to hear you find it useful!
@classicrockonly
@classicrockonly 5 жыл бұрын
This is probably the first video that has made decorators make sense to me. Thank you!
5 жыл бұрын
Glad you found it useful!
@audioplatform6299
@audioplatform6299 6 жыл бұрын
Just want to say 2 words... "Thank you".... was trying to understand this for a while and finally i got it!!
@PrakashReddyK
@PrakashReddyK 5 жыл бұрын
Thanks for the wonderful job of explaining the decorators in a precise and crystal clear way. Thanks a ton
@naveenkalhan95
@naveenkalhan95 4 жыл бұрын
watched lots of tutorials that were explaining the python decorators!!! had to close videos one by one because they were explaining in such a complicated way! you really explained the same in such a simple way,... that even a non-programmer can understand as well :)) thank you very much
@rahulmistri1997
@rahulmistri1997 3 жыл бұрын
Thank you it was really a concise explaination. Subscribed
@AyushMandowara_xx7
@AyushMandowara_xx7 4 жыл бұрын
Hey, Very nicely explained! I finally understand what decorators are. One small thing though, what you are returning is actually PascalCase and not camelCase. Thanks!
@fuanka1724
@fuanka1724 6 жыл бұрын
Decorators can be quite complex sometimes, but this intro is perfect. Great video as always, thanks. Looking forward to seeing more on this topic.
@minhhuynhthe
@minhhuynhthe Жыл бұрын
Great work🎉!
@baldebalde9790
@baldebalde9790 3 жыл бұрын
Finally got the concept, thanks
@jameslovering9158
@jameslovering9158 5 жыл бұрын
Thanks very clear and I now understand alittle more today.
@abhisek.g.mahapatra
@abhisek.g.mahapatra 5 жыл бұрын
Concise and very well explained. Thanks for your efforts in creating these videos - really helpful.
@alexandermedina4950
@alexandermedina4950 2 жыл бұрын
Great explanation, thank you.
@SuperAcousticDude
@SuperAcousticDude 3 жыл бұрын
Very well explained!
@cxsey8587
@cxsey8587 2 жыл бұрын
Very useful and practical
@jameszaman3326
@jameszaman3326 Жыл бұрын
That was a VERY good tutorial!
@nerdvananc
@nerdvananc 3 жыл бұрын
Thanks a ton! I watched your video on asyncio and immediately subscribed, and now this is the fifth video on your channel I'm binging. I had a question though; adding the @mapper decorator to camelcase() works very well for when its passed lists, but breaks the original functionality camelcase() was intended for: which was to convert *strings* into CamelCase. With the @mapper decorator, writing camelcase("some_string") returns a list of each of "some_strings" letters capitalized into a list. Isn't this incorrect?
@vilmm
@vilmm 4 жыл бұрын
Excellent video
@officesuperhero9611
@officesuperhero9611 6 жыл бұрын
Just wanted to say thanks for this and other excellent videos. Great job!!
6 жыл бұрын
Thanks!
@arturomtz8
@arturomtz8 3 жыл бұрын
Keep up the good work! New subscriber from Mexico!
@wanderwithniraj
@wanderwithniraj 5 жыл бұрын
super one of the best explanation of decorator. Loved it! :)
@vatsal_gamit
@vatsal_gamit 3 жыл бұрын
This was amazing 🎉
@pradeep-he9jq
@pradeep-he9jq 3 жыл бұрын
Thank you. This was pretty clear explanation.
@bestest43
@bestest43 5 жыл бұрын
hi sir, what I did not understand what you have done is when I tried to put the first input as a string it gave me an error again. Maybe we handled list input problem but we lost string input control. It seems we changed the entire function internal structure. Instead of defining a decorator, I could define a list splitter function like you did and put that function before splitting the underscored string. Am I wrong? I did not get the purpose of a decorator.
@paulburger9904
@paulburger9904 4 жыл бұрын
I hate to be that guy, but this is actually Pascal case. Great tutorial by the way.
@Dualphase90
@Dualphase90 6 жыл бұрын
Sebastian Mathot back with the hip-hop names :)
@TheSuperHombre
@TheSuperHombre 4 жыл бұрын
Nice explanation, thanks a lot.
@juliamihet4237
@juliamihet4237 3 жыл бұрын
Awesome video! Just a small remark, ThisIsPascalCase, thisIsCamelCase (at least according to Microsoft) and this_is_snake_case. And this-is-kebab-case.
@rhyanz46
@rhyanz46 5 жыл бұрын
great tutorial,
@sunendran
@sunendran 5 жыл бұрын
This is the perfect explanation.
@slimyelow
@slimyelow 5 жыл бұрын
This is really another Kickazz le$$on, watch, do and learn !!
@SamSarwat90
@SamSarwat90 5 жыл бұрын
Dude, you are gold. Appreciated!
@onlysolo
@onlysolo 5 жыл бұрын
good video, clear explanations:)
@charlesbovalis6591
@charlesbovalis6591 5 жыл бұрын
By the way - at 2:35 of this video - tried to follow along and typed the code as I see it .. and I was getting a lot of Name Errors ( It was complaining that "another_function" was not defined .. so the solution was to re-arrange the functions order as follows: def turn_into_another_function(fn): return another_function def another_function(): print('another function') def a_function(): print('a function') a_function = turn_into_another_function(a_function) a_function() Now it works ...
5 жыл бұрын
That's correct. I made a mistake in the video, but the downloadable notebook is correct. (In the video there is no error because of the way that the namespace is preserved in a notebook.)
@charlesbovalis6591
@charlesbovalis6591 5 жыл бұрын
@ Thank you for your response Sebastian. I usually don't download stuff as I have plenty of "tools" already installed on my mac and so I type code as I see it on videos either straight on my interactive Python shell or using any of my code editors. Thank you for getting back to me. Nevertheless it was a good "challenge" to figure this out.
@sirberbe
@sirberbe 6 жыл бұрын
Thank you for your effort in doing excellent and useful videos I've learned a lot of things
6 жыл бұрын
Glad you learned something!
@Logan.Infinito
@Logan.Infinito 5 жыл бұрын
Rappers in the camelcase, you rock !
@nivethanyogarajah1493
@nivethanyogarajah1493 Жыл бұрын
Perfection
@ArjunUmathanu
@ArjunUmathanu 5 жыл бұрын
I do not understand how your decorator function (turn_into_another_function) returns the function (another_function) which is not declared yet in it's global scope. For me, I have to define the another_function above all. Only then I get the decorator works. Else, I get that function not defined error!!. Please correct me if I'm wrong.
5 жыл бұрын
That's correct-it's a bug in the video, so to say. The reason that it works in the video is that `another_function()` was already defined in the workspace because of what I did before. But if you run it from scratch you'll indeed get this error. This has been fixed in the notebook that you can find under the link below the video.
@ArjunUmathanu
@ArjunUmathanu 5 жыл бұрын
@ thank you
@曾玮-d8z
@曾玮-d8z Жыл бұрын
If 2:30 NameError: name 'another_function' is not defined. Did you mean: 'turn_into_another_function'? Solvation: move 'another_function' up to the most top
@syncwork
@syncwork 2 жыл бұрын
How to get a mindset to written python code in such a small lines? For example, Below is the code snippet of camelcase function if it was written by me. def camelcase(s): # turn "this_is_a_string" into "ThisIsAString" splited_list = s.split('_') print(splited_list) camelstring = "" for word in splited_list: capital_word = word.capitalize() camelstring += "".join(capital_word) print(camelstring)
@sm4shqsm4shq60
@sm4shqsm4shq60 6 жыл бұрын
Can someone explain why the original camelcase() isn't just hardcoded to behave differently in the first place? I get this is about dynamically altering functions, but I have a hard time seeing practical use in this, especially with the @syntax. For example, after applying the @mapper decorator, the original functionality of camelcase() is completely lost, no? Unless @mapper can somehow be "turned off", it can no longer handle a single string the way it used to...? This topic has me frustrated...
6 жыл бұрын
You never *need* decorators, because, as you say, you can also write a new function or directly modify the original function. However, a decorator is a convenient way to add functionality to many different functions. Caching is a good example of this. Say that you have thirty functions that all perform time-consuming operations. To speed things up, you could then build caching into each function separately. But a decorator allows you to do this more elegantly, by applying a caching decorator to all the functions. So that would be one use case. Such use cases, where decorators really help, are fairly rare, but you do run across them every once in a while!
@vvega5171
@vvega5171 5 жыл бұрын
Hi. Thank you very much for this explanation. Now I am very clear with Decorators. One last thing. What if I want to use the function for only one string?. For instance 'hello_world'. The output is not the one I expected. I got this ['H', 'E', 'L', 'L', 'O', '', 'W', 'O', 'R', 'L', 'D'] and not this HelloWorld I assume the purpose of the Decorator is to use the function only for list of strings and not for a single string.
5 жыл бұрын
In Python, a string is very similar to a list of individual characters, and that's how the decorated function is treating it. If you want to process a single string, you can turn it into a list of only one ['string']. Or rewrite the decorator so that it checks whether the argument is a list or a string, and then handles the situation accordingly!
@vvega5171
@vvega5171 5 жыл бұрын
​@ Thanks again! That is what I wanted to know. That I will never use the decorated function anymore. Instead, I will use the decorator functionality.
@LuvxJacqu4li8e
@LuvxJacqu4li8e Жыл бұрын
Thanks
@tshaylatte9502
@tshaylatte9502 4 жыл бұрын
Thank you!
@felipica
@felipica 2 жыл бұрын
valeu mano ajudou demais tmj
@santoshloke
@santoshloke 4 жыл бұрын
b-e-a-utiful
@sheikhakbar2067
@sheikhakbar2067 3 жыл бұрын
I keep coming to this video when I forget how decorators work.
@CodingMazaa
@CodingMazaa 6 жыл бұрын
Brilliant!! awesome!!!
@liridonsejdiu6110
@liridonsejdiu6110 5 жыл бұрын
you sir made my day thnx
@Lapinoire
@Lapinoire 4 жыл бұрын
I think i understand the premise of a decorator, but i don't understand why you wouldn't just for loop through the list like: for name in names: camelcase(name) print(name) I understand its a decorator explanation, but don't see the value in using a decorator?
@diouranke
@diouranke 5 жыл бұрын
Is this related to assert
5 жыл бұрын
I'm assuming that you're referring to the `assert` statement, which is a kind of sanity check that crashes the program when a certain condition is not met. So no, that's not really related to decorators!
@michaelshort2388
@michaelshort2388 Жыл бұрын
Camel case actually has the first word with no capital. :)
@renegadevi8882
@renegadevi8882 6 жыл бұрын
I find the @staticmethod pretty useful.
6 жыл бұрын
It is! I actually have a video on the topic: kzbin.info/www/bejne/q6HQgHSGoJeWgZo
@mafridi106
@mafridi106 6 жыл бұрын
I keep getting this error: # python test.py Traceback (most recent call last): File "test.py", line 6, in a_function = turn_into_another_function(a_function) File "test.py", line 2, in turn_into_another_function return another_function NameError: global name 'another_function' is not defined This is what the code looks like: def turn_into_another_function(fnc): return another_function def a_function(): print('a function') a_function = turn_into_another_function(a_function) def another_function(): print('another function') a_function()
6 жыл бұрын
There was a bug in the notebook which I hadn't noticed! The fixed notebook is here: osf.io/szwhk/
6 жыл бұрын
There was a bug in the original notebook, but it's fixed now! osf.io/szwhk/
@yuliu1105
@yuliu1105 6 жыл бұрын
Hello Sebastiaan Mathôt, Thank you so much for this great tutorial. I don't quite understand one syntax in @mapper, func(value) for value in list_of_values, what is func(value)? Also, I believe camelcase is turn string_like_this to stringLikeThis. So this is my camelcase func def camelcase(text): a=[word for word in text.split('_')] return a[0].lower()+''.join([a[i].title() for i in range(1, len(a))])
6 жыл бұрын
fnc(value) is the actual function call, where fnc is the decorated function, and value is the function argument. And this is embedded in a list comprehension so that fnc is called once for each value from list_of_values. Does that make sense? And you're right about camelCase :-)
@yuliu1105
@yuliu1105 6 жыл бұрын
can I understand this way? func(value) is actually calling camelcase(s)? decorator is complicated.
6 жыл бұрын
Yes, exactly!
@vincentpiegsa
@vincentpiegsa 6 жыл бұрын
Great video, but I get an error when executing the first example: line 3, in turn_into_another_function return another_function NameError: name 'another_function' is not defined I already checked for spelling mistakes, but everything seems to be fine. Can anyone help me with that?
6 жыл бұрын
Ah, thanks for pointing this out! I fixed the notebook: osf.io/szwhk/
@debvdo
@debvdo 6 жыл бұрын
I wonder how it worked out in the video (2:40). You are defining another_function() after applying the decorator. Why didn't it give a NameError?
6 жыл бұрын
That's simply because another_function had already been defined in the workspace during a previous execution of the cell. But when running from an empty workspace it indeed couldn't work.
@kevinchang2
@kevinchang2 3 жыл бұрын
This is a really good tutorial but I still dont get it unfortunately
@erikschiegg68
@erikschiegg68 6 жыл бұрын
👍
@havehalkow
@havehalkow Жыл бұрын
Function, a function, another function, function, function decorator dizzy.
@ikhurramraza
@ikhurramraza 5 жыл бұрын
But why would you do this? Why not just update the camelcase function to accept list instead?
5 жыл бұрын
In general, decorators allow you to apply some common functionality to many functions, without having to explicitly implement this in each of the functions. So it's about modularity. Of course, this camelcase example is too trivial to really highlight the usefulness of decorators. But imagine for example a 'memoization' decorator that caches the results of a time-consuming function.
@M.I.S
@M.I.S 2 жыл бұрын
Thank YOU!
Python Decorators 2: Decorators with arguments
9:32
Sebastiaan Mathôt
Рет қаралды 16 М.
Python tricks: properties, staticmethods, and classmethods
12:39
Sebastiaan Mathôt
Рет қаралды 44 М.
龟兔赛跑:好可爱的小乌龟#short #angel #clown
01:00
Super Beauty team
Рет қаралды 137 МЛН
When mom gets home, but you're in rollerblades.
00:40
Daniel LaBelle
Рет қаралды 141 МЛН
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 123 МЛН
HELP!!!
00:46
Natan por Aí
Рет қаралды 52 МЛН
Decorators in Python (Easy to Understand Guide) #31
15:23
Programiz
Рет қаралды 59 М.
Python tricks: Demystifying async, await, and asyncio
15:59
Sebastiaan Mathôt
Рет қаралды 94 М.
What do programmers actually do?
9:20
Physics Girl
Рет қаралды 2,1 МЛН
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 450 М.
Understanding decorators [Python tutorial]
42:31
Clear Code
Рет қаралды 26 М.
Python Decorators: The Complete Guide
27:59
ArjanCodes
Рет қаралды 158 М.
Python Decorators 3: Turning a function into a class instance
5:31
Sebastiaan Mathôt
Рет қаралды 12 М.
The Single Most Useful Decorator in Python
3:50
mCoding
Рет қаралды 380 М.
龟兔赛跑:好可爱的小乌龟#short #angel #clown
01:00
Super Beauty team
Рет қаралды 137 МЛН