WEBVTT 1 00:00:00.110 --> 00:00:01.100 Hi welcome back, 2 00:00:01.100 --> 00:00:02.100 in this video we're gonna look 3 00:00:02.100 --> 00:00:05.110 at how to keep our function names and docstrings 4 00:00:05.110 --> 00:00:06.560 when we use a decorator. 5 00:00:07.420 --> 00:00:08.890 It's really straightforward, 6 00:00:08.890 --> 00:00:11.260 all we have to do is tell Python 7 00:00:11.260 --> 00:00:12.880 that this function here 8 00:00:13.780 --> 00:00:18.350 is actually wrapping around our original function, func 9 00:00:19.360 --> 00:00:20.640 because that's what it's doing. 10 00:00:20.640 --> 00:00:22.510 This function does a bunch of things 11 00:00:22.510 --> 00:00:25.500 before running the original function 12 00:00:25.500 --> 00:00:26.920 so you could think of it as a wrapper 13 00:00:26.920 --> 00:00:29.030 or a bubble around the original function, 14 00:00:29.030 --> 00:00:32.470 adding some functionality to it, 15 00:00:32.470 --> 00:00:34.550 so if we tell it that this, 16 00:00:34.550 --> 00:00:37.100 this function here wraps around this one, 17 00:00:37.100 --> 00:00:39.210 it will say "ah, okay I understand, 18 00:00:39.210 --> 00:00:40.250 "what you want to do is sort 19 00:00:40.250 --> 00:00:43.390 "of extend the functionality of this function, 20 00:00:43.390 --> 00:00:46.060 "but you still want to keep the name and the docstring," 21 00:00:46.060 --> 00:00:47.840 and in order to that, very straightforward, 22 00:00:47.840 --> 00:00:52.091 all we have to do is import functools. 23 00:00:52.091 --> 00:00:54.770 Function Tools is a package that comes with Python 24 00:00:54.770 --> 00:00:57.571 and then add a decorator in here, 25 00:00:57.571 --> 00:01:01.520 functools dot wraps func. 26 00:01:01.520 --> 00:01:02.890 It's funny how you have to use a decorator 27 00:01:02.890 --> 00:01:06.440 to extend the functionality of a decorated function, 28 00:01:06.440 --> 00:01:07.830 but nonetheless, what this does is say 29 00:01:07.830 --> 00:01:10.860 "hey, secure func, you are wrapping 30 00:01:10.860 --> 00:01:13.070 "around this original function, 31 00:01:13.070 --> 00:01:15.460 "so keep the name and the docstring." 32 00:01:15.460 --> 00:01:16.670 Now when we run this, 33 00:01:18.070 --> 00:01:20.840 we can see that the names that we printed out down here 34 00:01:20.840 --> 00:01:23.310 are actually my function and another. 35 00:01:24.210 --> 00:01:27.010 So that's exactly what we wanted, 36 00:01:27.010 --> 00:01:28.670 we can now decorate multiple functions 37 00:01:28.670 --> 00:01:30.700 and they're each gonna keep their name 38 00:01:30.700 --> 00:01:32.850 and all is gonna be good, 39 00:01:32.850 --> 00:01:36.740 particularly when we use some libraries and frameworks, 40 00:01:36.740 --> 00:01:38.810 that look at the name of the function 41 00:01:38.810 --> 00:01:41.270 as opposed to just the variable name, 42 00:01:41.270 --> 00:01:43.400 we can have some problems if we don't do this, 43 00:01:43.400 --> 00:01:44.420 so my recommendation 44 00:01:44.420 --> 00:01:47.600 is always use functools wraps. 45 00:01:47.600 --> 00:01:49.000 It's just gonna make your life easier 46 00:01:49.000 --> 00:01:51.780 and it really doesn't take a lot of effort. 47 00:01:51.780 --> 00:01:53.000 So that's it for this video 48 00:01:53.000 --> 00:01:54.650 and I'll see you on the next one.