What is a Python Decorator?
45sOpens with a clear, intriguing promise to explain a powerful Python feature, hooking beginners.
▶ Play ClipThis video explains Python decorators, a feature that allows adding extra functionality to existing functions without modifying their code. Using a division function example, the presenter demonstrates how to swap arguments when the numerator is smaller than the denominator, illustrating how decorators can alter function behavior at compile time.
The video introduces Python decorators as a way to add extra features to existing functions without changing their code.
A simple division function is created that takes two parameters and returns their quotient.
The presenter wants to ensure the numerator is always larger than the denominator, swapping values if necessary, without modifying the original division function.
A decorator function 'smart_div' is created that accepts the original function, defines an inner function to swap arguments if needed, and returns the inner function.
The decorator is applied by reassigning the original function name to the result of the decorator, effectively changing its behavior.
Decorators allow changing function behavior at compile time by passing functions as parameters and defining inner functions.
Python decorators provide a powerful way to modify or extend the behavior of functions without altering their source code, leveraging Python's support for functional programming and first-class functions.
"The title accurately describes the tutorial content on Python decorators for beginners."
What is a decorator in Python?
A decorator is a function that takes another function as an argument and extends its behavior without modifying the original function.
3:03
How do you apply a decorator to a function?
By reassigning the function name to the result of the decorator: func = decorator(func).
5:21
What is the purpose of the inner function inside a decorator?
The inner function contains the additional logic that modifies the behavior of the original function.
3:50
What does the decorator function return?
The decorator function returns the inner function.
5:06
Why can Python use decorators?
Because Python supports functional programming and treats functions as first-class objects, allowing them to be passed as arguments and defined inside other functions.
6:59
Decorator Definition
Clearly defines what a decorator is and its purpose.
3:03Decorator Benefit
Highlights that decorators can change function behavior at compile time.
6:45[00:01] [Music]
[00:07] welcome back aliens my name is Devin
[00:09] Wendy and in this video we'll talk about
[00:11] one of the amazing feature of Python
[00:13] which is decorators nothing about this
[00:16] when you talk about functions functions
[00:18] are built to perform certain tasks right
[00:21] so example let's say we have this file
[00:23] and in this file we have a function
[00:25] which is predefined so let's get a
[00:27] function here and this will be for
[00:29] division of course you can write any
[00:30] complex code but just to keep it simple
[00:32] let's go for division and of course a
[00:34] division function takes two parameters
[00:36] so we'll take a and B and this will
[00:39] return the division of this two so we'll
[00:41] say return a divided by B simple code
[00:44] right of course you can make it complex
[00:46] you can go for complex code by just to
[00:47] keep it simple the got two lines a
[00:49] division which divides two numbers and
[00:51] then if you call this function if you
[00:53] say let's see if I pass for my two and
[00:56] if you run this code of course you have
[00:58] to also print and short frittering here
[00:59] let's print so if you call this function
[01:02] now let's right-click and say run you
[01:05] can see we got two points so this is
[01:06] what we wanted right we wanted the
[01:08] output okay that's great but what if if
[01:11] I pass the value which is two and four
[01:15] so what are you expecting the output
[01:17] here so the output which I'm expecting
[01:19] here is of course 0.5 because that's
[01:21] what you will get and you can see we
[01:23] call 0.5 so what if I say I want a
[01:26] different logic here the logic which I
[01:28] want is doesn't matter in which sequence
[01:31] I pass the value it should be always the
[01:34] numerator should be bigger than the
[01:35] denominator so example in this case if
[01:37] I'm passing 2 and 4 it should be reverse
[01:39] while dividing only when my numerator is
[01:42] less than denominator I want to swap
[01:44] them ok so in this case even if you are
[01:47] passing 2 and footage the dividend
[01:48] should be 4 divided by 2 okay so we can
[01:51] do that right we can have our own logic
[01:53] so just for the example we are going for
[01:55] this ok so what do you think what you do
[01:57] so of course as a user I should be
[02:00] passing 4 and 2 but let's say as a user
[02:02] I want to pass 2 and 4 it is your job
[02:04] developer to make sure that they are
[02:07] getting swept okay you will say ok it's
[02:09] my job I will do it so you will go to
[02:11] the existing code with the division and
[02:13] you
[02:13] say okay so before dividing those two
[02:16] numbers I want to apply a logic the
[02:18] logic is if a is less than B I just want
[02:23] to swap them and we know how to swap two
[02:24] numbers right we can simply say a comma
[02:26] B is equal to B comma a simple logic and
[02:30] then once you have done that if you're
[02:31] on this code you can see we called 2.0
[02:33] it was so simple right but we got a
[02:36] twist the twist is as I mentioned
[02:38] imagine this code the division code
[02:41] which you have here is not with you this
[02:44] is in some other file and you're
[02:46] importing it maybe you don't have the
[02:48] access for this function and maybe you
[02:51] don't want to change the code of the
[02:53] existing function so I want you to swap
[02:56] those two values without touching the
[02:58] new function is it possible and that's
[03:01] where decorators comes into picture so
[03:03] what the decorators
[03:04] so using decorators you can add the
[03:06] extra features in the existing functions
[03:10] I know that sounds weird but we can do
[03:12] that so just to explain that whatever
[03:14] you do is either create a new function
[03:15] now this will be a decorator for teams I
[03:18] will say we're quite a smart because it
[03:20] can change the code right so it's a
[03:21] smart Dave now what this smart do will
[03:24] get now smart Dave will do as the
[03:28] function or the parameter so let's pass
[03:31] the function here so it will accept a
[03:33] function again while we talk about that
[03:35] later so this mod deal will accept a
[03:37] function and then so if you want to
[03:40] change the logic you have to write a
[03:41] code right I want to do that in another
[03:43] function so we can write a function
[03:45] inside a function that's the beauty of
[03:48] Python so let's create a function here
[03:50] so we'll say def we can have any
[03:52] function name let's go for dinner
[03:53] because they're in a function and then
[03:55] this inner function will take the same
[03:57] parameter which is taken by Dave two
[03:59] parameters let's take a comma B the name
[04:02] should not be same but you can have any
[04:04] name but the number of parameter should
[04:06] be same so you are passing - you have to
[04:07] accept - now in this inner you can write
[04:10] the logic which you are trying to write
[04:11] in dev so which was if a is greater than
[04:14] P in this case you will swap them so
[04:17] swapping is a comma B is equal to B
[04:19] comma a just to remove these spaces
[04:21] there let's do that and that's okay
[04:24] which have a space here so you can see
[04:25] we are creating a smart function here
[04:27] which has enough function who is doing
[04:30] our job so this is the code which I want
[04:32] inside my dev now once you have done
[04:35] that you simply have to return so you
[04:37] have to return the function which you
[04:39] are accepting here basically you know
[04:41] you are actually calling Dave here so in
[04:43] the return you will pass that two values
[04:45] a and B the new values so the original
[04:47] values were 2 & 4 after swapping you're
[04:50] passing food & 2 so you are calling a
[04:53] function by passing these two values now
[04:56] this function is your D function but
[04:58] you'll be thinking how they are matching
[05:00] just give me some time and then after
[05:03] this you just have to return the inner
[05:06] function because that's the function
[05:07] which is actually doing the job for you
[05:08] okay
[05:09] but will this work let's try let's run
[05:12] this code and you can see you're still
[05:14] getting 0.5 is because there is no
[05:16] connection between this mod Dave and
[05:19] this Dave let's create that connection
[05:21] so what we can do is before calling the
[05:23] duty function we can say Dave is equal
[05:26] to let's make it smart so we can assign
[05:28] the function to a function because
[05:30] everything in Python is no object right
[05:32] so against the DV is equal to smart Dave
[05:34] in which you will passing the new
[05:36] function now this new function is the
[05:39] original function you have and this is a
[05:41] new one in fact you know just to give
[05:43] you an idea I'll make this as do one and
[05:46] let me call that function do one by
[05:48] passing two and four so basically we are
[05:50] calling div but indirectly okay so we
[05:53] are calling de one which is calling
[05:55] which is using this mod do function by
[05:57] passing the values it will swap the
[05:59] values in build and then it will
[06:00] actually call the do function at the end
[06:03] which will print the values let's run
[06:07] this code and you can see regard the
[06:09] output which is wrong that's weird
[06:11] okay I guess there is something wrong
[06:13] with the if condition my bad
[06:14] so if errors okay so as a programmer you
[06:18] always make mistakes right so you can
[06:19] see it should be less than right that's
[06:20] what you want to smell so if you have
[06:22] this code now or we got to that's what
[06:24] we wanted right in fact the amazing
[06:26] thing is you don't have to go for a new
[06:28] name you can actually replace with the
[06:30] original itself which is Dave Dave so it
[06:32] looks like we are calling this new
[06:34] function but no just before calling them
[06:37] we are
[06:37] changing the definition for D right so
[06:39] this is the old Dave we are making a new
[06:41] deal now so you can see record 2.0 the
[06:45] amazing part about decorators is you can
[06:46] change the behavior of the existing
[06:49] function at the compile time itself so
[06:52] that's the decorators for you so just to
[06:54] recap what we are doing is we are
[06:55] creating a new function which takes
[06:57] function as a parameter and that's the
[06:59] beauty that this is not possible in all
[07:01] the languages so yes we can do that in
[07:03] Python because python is also a
[07:05] functional programming and then we can
[07:07] define a function inside a function
[07:09] which is actually replacing the code of
[07:11] div behind the scene and then we before
[07:15] pausing deep we are saying Dave is equal
[07:17] to smart Dave and we are passing Dave so
[07:19] basically we are changing the way they
[07:21] works so that's how you can use
[07:23] decorators in Python I hope you got
[07:25] something about decorators in this video
[07:27] so I hope you are enjoying this series
[07:29] let me in the comment section and do
[07:31] subscribe for further videos
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.