{"id":655,"date":"2021-04-05T19:41:01","date_gmt":"2021-04-05T18:41:01","guid":{"rendered":"https:\/\/blog.thomarite.uk\/?p=655"},"modified":"2021-04-05T19:41:01","modified_gmt":"2021-04-05T18:41:01","slug":"decorators","status":"publish","type":"post","link":"https:\/\/blog.thomarite.uk\/index.php\/2021\/04\/05\/decorators\/","title":{"rendered":"Decorators"},"content":{"rendered":"\n<p>Trying to make a small change to a python program in my job, I came across something I didnt know in python and only after reading this <a href=\"https:\/\/towardsdatascience.com\/why-decorators-in-python-are-pure-genius-1e812949a81e\">article<\/a> (a couple of times) I managed to understand (ask me again in 3 weeks \ud83d\ude42 The best definition I could find was actually from other <a href=\"https:\/\/realpython.com\/primer-on-python-decorators\/\">blog<\/a> reference and actually I think it was more clear to me.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8220;A decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.&#8221;<\/p><\/blockquote>\n\n\n\n<p>1 &#8211; Function returning a function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In &#91;7]: def greet(name): \n   ...:     return f\"Hello, {name}!\" \n   ...: def simon(func): \n   ...:     return func(\"Simon\") \n   ...:                                                                                                                       \n\nIn &#91;8]: <strong>simon(greet)<\/strong>                                                                                                          \nOut&#91;8]: 'Hello, Simon!'\n\n<\/code><\/pre>\n\n\n\n<p><em>simon<\/em> is a function that has a function as argument, and calls that function with the argument &#8220;Simon&#8221;.  So the function <em>greet<\/em> is receiving the argument &#8220;Simon&#8221; and returns a string with that name.<\/p>\n\n\n\n<p>2 &#8211; Functions inside other functions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In &#91;9]: def respect(maybe): \n   ...:     def congrats(): \n   ...:         return \"Congrats, bro!\" \n   ...:     def insult(): \n   ...:         return \"You're silly!\" \n   ...:     if maybe == \"yes\": \n   ...:         return congrats \n   ...:     else: \n   ...:         return insult \n   ...:                                                                                                                       \n\nIn &#91;10]: <strong>respect(\"hola\")()<\/strong>                                                                                                    \nOut&#91;10]: \"You're silly!\"\n<\/code><\/pre>\n\n\n\n<p>In this case, to execute the returned function you need to use &#8220;()&#8221; at the end (because respect(&#8220;hola&#8221;) it is a function!): <em>respect(&#8220;hola&#8221;)()<\/em><\/p>\n\n\n\n<p>Keep in mind you return the function &#8220;congrats&#8221; and it is not executed. If you return &#8220;congrats()&#8221; then yes, you get the result of executing congrats.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>3 &#8211; A function that takes another function and defines a function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In &#91;1]: def startstop(func): \n   ...:     def wrapper(): \n   ...:         print(\"Starting...\") \n   ...:         func() \n   ...:         print(\"Finished!\") \n   ...:     return wrapper \n   ...: def roll(): \n   ...:     print(\"Rolling on the floor laughing XD\") \n   ...: <strong><em>roll = startstop(roll)<\/em> <\/strong>                                                                                               \n\nIn &#91;2]: <strong>roll()<\/strong>                                                                                                                \nStarting...\nRolling on the floor laughing XD\nFinished!\n\nIn &#91;3]:        <\/code><\/pre>\n\n\n\n<p>This case is the same as before, need to use &#8220;()&#8221; to &#8220;execute&#8221; roll because it is a function.<\/p>\n\n\n\n<p>4 &#8211; With decorator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In &#91;3]: def startstop(func): \n   ...:     def wrapper(): \n   ...:         print(\"Starting...\") \n   ...:         func() \n   ...:         print(\"Finished!\") \n   ...:     return wrapper \n   ...: <strong>@startstop<\/strong> \n   ...: def roll(): \n   ...:     print(\"Rolling on the floor laughing XD\") \n   ...:                                                                                                                       \n\nIn &#91;4]: <strong>roll()<\/strong>                                                                                                                \nStarting...\nRolling on the floor laughing XD\nFinished!\n\nIn &#91;5]:          <\/code><\/pre>\n\n\n\n<p>So adding the decorator we avoid writing a line with nested functions. That in a more advanced case, it would make the code easier to read.<\/p>\n\n\n\n<p>You can nest decorators and add arguments too.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trying to make a small change to a python program in my job, I came across something I didnt know in python and only after reading this article (a couple of times) I managed to understand (ask me again in 3 weeks \ud83d\ude42 The best definition I could find was actually from other blog reference &hellip; <a href=\"https:\/\/blog.thomarite.uk\/index.php\/2021\/04\/05\/decorators\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Decorators&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,25],"tags":[],"class_list":["post-655","post","type-post","status-publish","format-standard","hentry","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/comments?post=655"}],"version-history":[{"count":1,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"predecessor-version":[{"id":656,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/655\/revisions\/656"}],"wp:attachment":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}