{"id":295825,"date":"2021-07-10T17:00:30","date_gmt":"2021-07-10T14:00:30","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/decorators-in-python-will-make-your-code-so-much-better\/"},"modified":"2021-07-10T17: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:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/","title":{"rendered":"#Decorators in Python will make your code so much better"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a4174645055e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a4174645055e\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Before_anything_else_higher-order_functions\" >Before anything else: higher-order functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#The_ABC_of_Python_decorators\" >The ABC of Python decorators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Added_flexibility\" >Added flexibility<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Applications_where_decorators_cut_the_cream\" >Applications: where decorators cut the cream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Measuring_execution_time\" >Measuring execution time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Testing_and_debugging\" >Testing and debugging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Reusing_code\" >Reusing code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Handling_logins\" >Handling logins<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/decorators-in-python-will-make-your-code-so-much-better\/#Syntactic_sugar_%E2%80%94_or_why_Python_is_so_sweet\" >Syntactic sugar \u2014 or why Python is so sweet<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#Decorators in Python will make your code so much better<\/strong>&#8221;<\/p>\n<div>\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>\u00a0<\/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\"><span class=\"ez-toc-section\" id=\"Before_anything_else_higher-order_functions\"><\/span>Before anything else: higher-order functions<span class=\"ez-toc-section-end\"><\/span><\/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.\u00a0So 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>\u00a0<\/span><code>greet()<\/code><span>\u00a0<\/span>\u2014 it greets whatever object you pass it. And let\u2019s say you have another function,<span>\u00a0<\/span><code>simon()<\/code><span>\u00a0<\/span>\u2014 it inserts \u201cSimon\u201d wherever <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ropriate. 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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359982 aligncenter js-lazy\" alt=\"python\" width=\"680\" height=\"184\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.09.png\" 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 loading=\"lazy\" decoding=\"async\" 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 style=\"font-family: Consolas, Monaco, monospace;\"\/><\/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>\u00a0<\/span><code>'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>\u00a0<\/span><code>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>\u00a0<\/span><code>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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359984 aligncenter js-lazy\" alt=\"python\" width=\"677\" height=\"229\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.05.23.png\" 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 loading=\"lazy\" decoding=\"async\" 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\u00a0<code>respect()<\/code><span>\u00a0<\/span>returns a function;<span>\u00a0<\/span><code>respect(\"yes\")<\/code><span>\u00a0<\/span>returns the congrats function,<span>\u00a0<\/span><code>respect(\"brother\")<\/code><span>\u00a0<\/span>(or some other argument instead of<span>\u00a0<\/span><code>\"brother\"<\/code>) returns the insult function. To call the functions, enter<span>\u00a0<\/span><code>respect(\"yes\")()<\/code><span>\u00a0<\/span>and<span>\u00a0<\/span><code>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\"><span class=\"ez-toc-section\" id=\"The_ABC_of_Python_decorators\"><\/span><strong>The ABC of Python decorators<\/strong><span class=\"ez-toc-section-end\"><\/span><\/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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359983 aligncenter js-lazy\" alt=\"python\" width=\"678\" height=\"259\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.18.png\" 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 loading=\"lazy\" decoding=\"async\" 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>\u00a0<\/span><code>startstop(roll)()<\/code><span>\u00a0<\/span>anymore;<span>\u00a0<\/span><code>roll()<\/code><span>\u00a0<\/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>\u00a0<\/span><code>startstop()<\/code>:<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf\"><figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359986 aligncenter js-lazy\" alt=\"\" width=\"677\" height=\"90\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.08.08.png\" 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 loading=\"lazy\" decoding=\"async\" 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>\u00a0<\/span><code>roll()<\/code><span>\u00a0<\/span>to<span>\u00a0<\/span><code>startstop()<\/code><span>\u00a0<\/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=\"\"><span class=\"ez-toc-section\" id=\"Added_flexibility\"><\/span>Added flexibility<span class=\"ez-toc-section-end\"><\/span><\/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>\u00a0<\/span><code>def startstop()<\/code><span>\u00a0<\/span>part above) into its own module. That is, you write them into a file called<span>\u00a0<\/span><code>decorators.py<\/code><span>\u00a0<\/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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359985 aligncenter js-lazy\" alt=\"python\" width=\"678\" height=\"142\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.27.png\" 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 loading=\"lazy\" decoding=\"async\" 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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359987 aligncenter js-lazy\" alt=\"\" width=\"679\" height=\"158\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.33.png\" 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 loading=\"lazy\" decoding=\"async\" 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>\u00a0<\/span><code>exectime()<\/code><span>\u00a0<\/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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359988 aligncenter js-lazy\" alt=\"\" width=\"678\" height=\"55\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.39.png\" 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 loading=\"lazy\" decoding=\"async\" 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>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#decorating-functions-with-arguments\">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\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#decorators-with-arguments\">to your decorators<\/a><span>\u00a0<\/span>\u2014 like<span>\u00a0<\/span><code>@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\"><span class=\"ez-toc-section\" id=\"Applications_where_decorators_cut_the_cream\"><\/span>Applications: where decorators cut the cream<span class=\"ez-toc-section-end\"><\/span><\/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=\"\"><span class=\"ez-toc-section\" id=\"Measuring_execution_time\"><\/span>Measuring execution time<span class=\"ez-toc-section-end\"><\/span><\/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>\u00a0<\/span><code>waste time()<\/code>\u00a0and 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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359989 aligncenter js-lazy\" alt=\"\" width=\"677\" height=\"341\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.47.png\" 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 loading=\"lazy\" decoding=\"async\" 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>\u00a0<\/span><code>measuretime()<\/code><span>\u00a0<\/span>on as many functions as you want.<\/p>\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 im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely 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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359990 aligncenter js-lazy\" alt=\"\" width=\"681\" height=\"302\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-16.56.53.png\" 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 loading=\"lazy\" decoding=\"async\" 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>\u00a0<\/span><code>wakeup()<\/code><span>\u00a0<\/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=\"\"><span class=\"ez-toc-section\" id=\"Testing_and_debugging\"><\/span>Testing and debugging<span class=\"ez-toc-section-end\"><\/span><\/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 loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1359992 aligncenter js-lazy\" alt=\"\" width=\"678\" height=\"232\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-09-at-17.15.53.png\" 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 loading=\"lazy\" decoding=\"async\" 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\u00a0more elaborate example<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#debugging-code\">here<\/a>. Note, though, that to understand that example, you\u2019ll have to check<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#decorating-functions-with-arguments\">how to<\/a><span>\u00a0<\/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=\"\"><span class=\"ez-toc-section\" id=\"Reusing_code\"><\/span>Reusing code<span class=\"ez-toc-section-end\"><\/span><\/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\u00a0<code>decorator()<\/code>, you can just sprinkle<span>\u00a0<\/span><code>@decorator<\/code><span>\u00a0<\/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=\"\"><span class=\"ez-toc-section\" id=\"Handling_logins\"><\/span>Handling logins<span class=\"ez-toc-section-end\"><\/span><\/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>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw px\" href=\"https:\/\/realpython.com\/primer-on-python-decorators\/#is-the-user-logged-in\">full example<\/a><span>\u00a0<\/span>for reference, but the principle is quite simple: first, you define a function like<span>\u00a0<\/span><code>login_required()<\/code>. Before any function definition that needs logging in, you pop<span>\u00a0<\/span><code>@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\"><span class=\"ez-toc-section\" id=\"Syntactic_sugar_%E2%80%94_or_why_Python_is_so_sweet\"><\/span>Syntactic sugar \u2014 or why Python is so sweet<span class=\"ez-toc-section-end\"><\/span><\/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 like I\u2019m not<span>\u00a0<\/span>critical of Python<span>\u00a0<\/span>or not using<span>\u00a0<\/span>alternative languages<span>\u00a0<\/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>\u00a0<\/span><span data-sheets-value=\"{\" moutafis=\"\" data-sheets-userformat=\"{\">Rhea Moutafis\u00a0<\/span>and was originally published on<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>. You can read it<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/towardsdatascience.com\/why-decorators-in-python-are-pure-genius-1e812949a81e\">here<\/a>.\u00a0<\/em><\/p>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/news\/decorators-in-python-make-code-better-syndication\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Decorators in Python will make your code so much better&#8221; 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":295826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/python-code.jpg&signature=381734fcda63033359d3b1c53b875634","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-295825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/295825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=295825"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/295825\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/295826"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=295825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=295825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=295825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}