{"id":6446,"date":"2021-07-10T14:00:30","date_gmt":"2021-07-10T14:00:30","guid":{"rendered":"http:\/\/TheNextWeb=1359972"},"modified":"2021-07-10T14:00:30","modified_gmt":"2021-07-10T14:00:30","slug":"decorators-in-python-will-make-your-code-so-much-better","status":"publish","type":"post","link":"https:\/\/www.londonchiropracter.com\/?p=6446","title":{"rendered":"Decorators in Python will make your code so much better"},"content":{"rendered":"\n<p id=\"36c6\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if lv\" data-selectable-paragraph>If there\u2019s one thing that makes Python incredibly successful, that would be its readability. Everything else hinges on that: if code is unreadable, it\u2019s hard to maintain. It\u2019s also not beginner-friendly then \u2014 a novice getting boggled by unreadable code won\u2019t attempt writing its own one day.<\/p>\n<p id=\"90c4\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Python was already readable and beginner-friendly before decorators came around. But as the language started getting used for more and more things, Python developers felt the need for more and more features, without cluttering the landscape and making code unreadable.<\/p>\n<p id=\"18b2\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Decorators ar<span id=\"rmm\">e<\/span><span>&nbsp;<\/span>a prime-time example of a perfectly implemented feature. It does take a while to wrap your head around, but it\u2019s worth it. As you start using them, you\u2019ll notice how they don\u2019t overcomplicate things and make your code neat and snazzy.<\/p>\n<h2 id=\"e13e\" class=\"nk nl gx bf nm nn no ld np nq nr lg ns nt nu nv nw nx ny nz oa ob oc od oe of if\">Before anything else: higher-order functions<\/h2>\n<p id=\"07e8\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>In a nutshell, decorators are a neat way to handle higher-order functions.&nbsp;So let\u2019s look at those first!<\/p>\n<p id=\"1db5\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\"><strong>Functions returning functions<\/strong><\/p>\n<p id=\"6741\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Say you have one function,<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">greet()<\/code><span>&nbsp;<\/span>\u2014 it greets whatever object you pass it. And let\u2019s say you have another function,<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">simon()<\/code><span>&nbsp;<\/span>\u2014 it inserts \u201cSimon\u201d wherever appropriate. How can we combine the two? Think about it a minute before you look below.<\/p>\n<p class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359982 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09.png\" alt=\"python\" width=\"680\" height=\"184\" sizes=\"(max-width: 680px) 100vw, 680px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09-280x76.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09-540x146.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09-270x73.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359982 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09.png\" alt=\"python\" width=\"680\" height=\"184\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09-280x76.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09-540x146.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09-270x73.png 270w\"><\/noscript><\/figure>\n<p><span><\/span><\/p>\n<p id=\"19ec\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>The output is<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">'Hello, Simon!'<\/code>. Hope that makes sense to ya!<\/p>\n<p id=\"62ba\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Of course, we could have just called<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">greet(\"Simon\")<\/code>. However, the whole point is that we might want to put \u201cSimon\u201d into many different functions. And if we don\u2019t use \u201cSimon\u201d but something more complicated, we can save a whole lot of lines of code by packing it into a function like<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">simon()<\/code>.<\/p>\n<p id=\"dcde\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\"><strong>Functions inside other functions<\/strong><\/p>\n<p id=\"0722\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>We can also define functions inside other functions. That\u2019s important because decorators will do that, too! Without decorators it looks like this:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359984 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23.png\" alt=\"python\" width=\"677\" height=\"229\" sizes=\"(max-width: 677px) 100vw, 677px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23-280x95.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23-540x183.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23-270x91.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359984 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23.png\" alt=\"python\" width=\"677\" height=\"229\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23-280x95.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23-540x183.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23-270x91.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"43dd\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>The function&nbsp;<code class=\"ot ou ov ow ox b\">respect()<\/code><span>&nbsp;<\/span>returns a function;<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">respect(\"yes\")<\/code><span>&nbsp;<\/span>returns the congrats function,<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">respect(\"brother\")<\/code><span>&nbsp;<\/span>(or some other argument instead of<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">\"brother\"<\/code>) returns the insult function. To call the functions, enter<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">respect(\"yes\")()<\/code><span>&nbsp;<\/span>and<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">respect(\"brother\")()<\/code>, just like a normal function.<\/p>\n<p id=\"8a53\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Got it? Then you\u2019re all set for decorators!<\/p>\n<h2 id=\"02ab\" class=\"nk nl gx bf nm nn no ld np nq nr lg ns nt nu nv nw nx ny nz oa ob oc od oe of if\"><strong>The ABC of Python decorators<\/strong><\/h2>\n<p id=\"c49b\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\"><strong>Functions with an @ symbol<\/strong><\/p>\n<p id=\"9ef5\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Let\u2019s try a combination of the two previous concepts: a function that takes another function and defines a function. Sounds mind-boggling? Consider this:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359983 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18.png\" alt=\"python\" width=\"678\" height=\"259\" sizes=\"(max-width: 678px) 100vw, 678px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18-280x107.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18-540x206.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18-270x103.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359983 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18.png\" alt=\"python\" width=\"678\" height=\"259\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18-280x107.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18-540x206.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18-270x103.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"2624\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>The last line ensures that we don\u2019t need to call<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">startstop(roll)()<\/code><span>&nbsp;<\/span>anymore;<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">roll()<\/code><span>&nbsp;<\/span>will suffice. Do you know what the output of that call is? Try it yourself if you\u2019re unsure!<\/p>\n<p id=\"3269\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Now, as a very good alternative, we could insert this right after defining<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">startstop()<\/code>:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359986 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08.png\" alt width=\"677\" height=\"90\" sizes=\"(max-width: 677px) 100vw, 677px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08-280x37.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08-540x72.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08-270x36.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359986 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08.png\" alt width=\"677\" height=\"90\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08-280x37.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08-540x72.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08-270x36.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"e0bf\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>This does the same, but glues<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">roll()<\/code><span>&nbsp;<\/span>to<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">startstop()<\/code><span>&nbsp;<\/span>at the onset.<\/p>\n<h2 id=\"1e15\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\" data-selectable-paragraph>Added flexibility<\/h2>\n<p id=\"0f19\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Why is that useful? Doesn\u2019t that consume exactly as many lines of code as before?<\/p>\n<p id=\"6b1f\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>In this case, yes. But once you\u2019re dealing with slightly more complicated stuff, it gets really useful. For once, you can move all decorators (i.e. the<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">def startstop()<\/code><span>&nbsp;<\/span>part above) into its own module. That is, you write them into a file called<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">decorators.py<\/code><span>&nbsp;<\/span>and write something like this into your main file:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359985 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27.png\" alt=\"python\" width=\"678\" height=\"142\" sizes=\"(max-width: 678px) 100vw, 678px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27-280x59.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27-540x113.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27-270x57.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359985 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27.png\" alt=\"python\" width=\"678\" height=\"142\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27-280x59.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27-540x113.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27-270x57.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"baf9\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>In principle, you can do that without using decorators. But this way it makes life easier because you don\u2019t have to deal with nested functions and endless bracket-counting anymore.<\/p>\n<p id=\"60c6\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>You can also nest decorators:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359987 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33.png\" alt width=\"679\" height=\"158\" sizes=\"(max-width: 679px) 100vw, 679px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33.png 679w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33-280x65.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33-540x126.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33-270x63.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359987 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33.png\" alt width=\"679\" height=\"158\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33.png 679w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33-280x65.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33-540x126.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33-270x63.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"8bb9\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Note that we haven\u2019t defined<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">exectime()<\/code><span>&nbsp;<\/span>yet, but you\u2019ll see it in the next section. It\u2019s a function that can measure how long a process takes in Python.<\/p>\n<p id=\"103c\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>This nesting would be equivalent to a line like this:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359988 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39.png\" alt width=\"678\" height=\"55\" sizes=\"(max-width: 678px) 100vw, 678px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39-280x23.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39-540x44.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39-270x22.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359988 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39.png\" alt width=\"678\" height=\"55\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39-280x23.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39-540x44.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39-270x22.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"b4d1\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Bracket counting is starting! Imagine you had five or six of those functions nested inside each other. Wouldn\u2019t the decorator notation be much easier to read than this nested mess?<\/p>\n<p id=\"ad5a\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>You can even use decorators on functions that<span>&nbsp;<\/span><a class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#decorating-functions-with-arguments\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">accept arguments<\/a>. Now imagine a few arguments in the line above and your chaos would be complete. Decorators make it neat and tidy.<\/p>\n<p id=\"7d2f\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Finally, you can even add arguments&nbsp;<a class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#decorators-with-arguments\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">to your decorators<\/a><span>&nbsp;<\/span>\u2014 like<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">@mydecorator(argument)<\/code>. Yeah, you can do all of this without decorators. But then I wish you a lot of fun understanding your decorator-free code when you re-read it in three weeks\u2026<\/p>\n<h2 id=\"bc95\" class=\"nk nl gx bf nm nn no ld np nq nr lg ns nt nu nv nw nx ny nz oa ob oc od oe of if\">Applications: where decorators cut the cream<\/h2>\n<p id=\"6f73\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Now that I\u2019ve hopefully convinced you that decorators make your life three times easier, let\u2019s look at some classic examples where decorators are basically indispensable.<\/p>\n<h2 id=\"864d\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\" data-selectable-paragraph>Measuring execution time<\/h2>\n<p id=\"aad5\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Let\u2019s say we have a function called<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">waste time()<\/code>&nbsp;and we want to know how long it takes. Well, just use a decorator!<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359989 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47.png\" alt width=\"677\" height=\"341\" sizes=\"(max-width: 677px) 100vw, 677px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47-280x141.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47-536x270.png 536w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47-268x135.png 268w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359989 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47.png\" alt width=\"677\" height=\"341\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47-280x141.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47-536x270.png 536w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47-268x135.png 268w\"><\/noscript><\/figure><\/pre>\n<p id=\"a309\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>A dozen lines of code and we\u2019re done! Plus, you can use<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">measuretime()<\/code><span>&nbsp;<\/span>on as many functions as you want.<\/p>\n<div class=\"mm mn mo mp mq mr\">\n<div class=\"dj n z\">\n<p><h2 class=\"py s ng nh ni ne nj kt mr\">Slowing code down<\/h2>\n<\/p>\n<\/div>\n<\/div>\n<p id=\"c2c9\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Sometimes you don\u2019t want to execute code immediately but wait a while. That\u2019s where a slow-down decorator comes in handy:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359990 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53.png\" alt width=\"681\" height=\"302\" sizes=\"(max-width: 681px) 100vw, 681px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53.png 681w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53-280x124.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53-540x239.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53-270x120.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359990 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53.png\" alt width=\"681\" height=\"302\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53.png 681w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53-280x124.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53-540x239.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53-270x120.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"f362\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>Calling<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">wakeup()<\/code><span>&nbsp;<\/span>makes lets you take a 5-minute break, after which your console reminds you to get back to work.<\/p>\n<h2 id=\"aa2a\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\" data-selectable-paragraph>Testing and debugging<\/h2>\n<p id=\"f986\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>Say you have a whole lot of different functions that you call at different stages, and you\u2019re losing the overview over what\u2019s being called when. With a simple decorator for every function definition, you can bring more clarity. Like so:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359992 aligncenter js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53.png\" alt width=\"678\" height=\"232\" sizes=\"(max-width: 678px) 100vw, 678px\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53-280x96.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53-540x185.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53-270x92.png 270w\"><noscript><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1359992 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53.png\" alt width=\"678\" height=\"232\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53-280x96.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53-540x185.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53-270x92.png 270w\"><\/noscript><\/figure><\/pre>\n<p id=\"c424\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>There is a&nbsp;more elaborate example<span>&nbsp;<\/span><a class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#debugging-code\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">here<\/a>. Note, though, that to understand that example, you\u2019ll have to check<span>&nbsp;<\/span><a class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#decorating-functions-with-arguments\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">how to<\/a><span>&nbsp;<\/span>decorate functions with arguments. Still, it\u2019s worth the read!<\/p>\n<h2 id=\"77bd\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\" data-selectable-paragraph>Reusing code<\/h2>\n<p id=\"5309\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>This kinda goes without saying. If you\u2019ve defined a function&nbsp;<code class=\"ot ou ov ow ox b\">decorator()<\/code>, you can just sprinkle<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">@decorator<\/code><span>&nbsp;<\/span>everywhere in your code. To be honest, I don\u2019t think it gets any simpler than that!<\/p>\n<h2 id=\"fa20\" class=\"ol nl gx bf nm om on ij np oo op im ns in oq ip nw iq or is oa it os iv oe he if\" data-selectable-paragraph>Handling logins<\/h2>\n<p id=\"fbf2\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>If you have functionalities that should only be accessed if a user is logged in, that\u2019s also fairly easy with decorators. I\u2019ll refer you to the<span>&nbsp;<\/span><a class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#is-the-user-logged-in\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">full example<\/a><span>&nbsp;<\/span>for reference, but the principle is quite simple: first, you define a function like<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">login_required()<\/code>. Before any function definition that needs logging in, you pop<span>&nbsp;<\/span><code class=\"ot ou ov ow ox b\">@login_required<\/code>. Simple enough, I\u2019d say.<\/p>\n<h2 id=\"3c45\" class=\"nk nl gx bf nm nn no ld np nq nr lg ns nt nu nv nw nx ny nz oa ob oc od oe of if\">Syntactic sugar \u2014 or why Python is so sweet<\/h2>\n<p id=\"4a66\" class=\"kz la gx lb b ih og ld le ik oh lg lh li oi lk ll lm oj lo lp lq ok ls lt lu gq if\" data-selectable-paragraph>It\u2019s not <a href=\"https:\/\/thenextweb.com\/news\/why-python-not-programming-language-future-syndication\">like I\u2019m not<span>&nbsp;<\/span>critical of Python<\/a><span>&nbsp;<\/span>or not <a href=\"https:\/\/thenextweb.com\/news\/watch-out-python-julia-programming-coding-language-coming-for-crown-syndication\">using<span>&nbsp;<\/span>alternative languages<\/a><span>&nbsp;<\/span>where it\u2019s appropriate. But there\u2019s a big allure to Python: it\u2019s so easy to digest, even when you\u2019re not a computer scientist by training and just want to make things work.<\/p>\n<p id=\"dcf5\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>If C++ is an orange, then Python is a pineapple: similarly nutritious, but three times sweeter. Decorators are just one factor in the mix.<\/p>\n<p id=\"48d2\" class=\"kz la gx lb b ih lc ld le ik lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu gq if\" data-selectable-paragraph>But I hope you\u2019ve come to see why it\u2019s such a big sweet-factor. Syntactic sugar to add some pleasure to your life! Without health risks, except for having your eyes glued on a screen.<\/p>\n<p data-selectable-paragraph><em>This article was written by<span>&nbsp;<\/span><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Rhea Moutafis&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:4227,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16777215},&quot;10&quot;:2,&quot;15&quot;:&quot;arial,sans,sans-serif&quot;}\">Rhea Moutafis&nbsp;<\/span>and was originally published on<span>&nbsp;<\/span><a href=\"https:\/\/towardsdatascience.com\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Towards Data Science<\/a>. You can read it<span>&nbsp;<\/span><a href=\"https:\/\/towardsdatascience.com\/why-decorators-in-python-are-pure-genius-1e812949a81e\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">here<\/a>.&nbsp;<\/em><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/news\/decorators-in-python-make-code-better-syndication\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If there\u2019s one thing that makes Python incredibly successful, that would be its readability. Everything else hinges on that: if code is unreadable, it\u2019s hard to maintain. It\u2019s also not beginner-friendly then&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6447,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/6446"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6446"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/6446\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/6447"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}