{"id":6183,"date":"2022-11-02T05:16:38","date_gmt":"2022-11-02T05:16:38","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6183"},"modified":"2022-11-02T05:46:18","modified_gmt":"2022-11-02T05:46:18","slug":"decorators-with-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/decorators-with-python\/","title":{"rendered":"Decorators with Python"},"content":{"rendered":"<p>There is a very interesting feature in python known as Decorators, that is used to add additional functionality to the existing code.<\/p>\n<p>Because this feature tries to modify the existing code functionality, this is known as metaprogramming.<\/p>\n<h2>Things that one should know before starting with decorators<\/h2>\n<p>One must be completely familiar with the concept of python, that everything in python is an object. These objects can be recognized using names that we give to objects. Functions are also objects with attributes. One can give many diff names to the same function object.<br \/>\n<code><strong>e.g.<\/strong><br \/>\ndef one(a):<br \/>\nprint(a)<br \/>\none(\u2018asd\u2019)<br \/>\ntwo=one<br \/>\ntwo(\u2018asd\u2019)0<br \/>\n<strong>o\/p:<\/strong><br \/>\nasd<br \/>\nasd<br \/>\n<\/code><\/p>\n<p>So in the above example, both functions <strong>one<\/strong> and <strong>two <\/strong>give the same result because both names refer to the same function object.<\/p>\n<p>After that things are getting a bit more complicated. Because a function can be passed to another function as an argument.<br \/>\nFor example, we use <strong>reduce <\/strong>and <strong>filter <\/strong>functions in python.<br \/>\nFunctions like these can take other functions as arguments are known as<strong> higher-order<\/strong> functions.<br \/>\n<code>e.g.<br \/>\n<strong>make functions<\/strong><br \/>\ndef increment(z):<br \/>\nreturn z + 1<br \/>\ndef decrement(z):<br \/>\nreturn z - 1<br \/>\ndef main(func, z):<br \/>\nresult = func(z)<br \/>\nreturn result<\/code><\/p>\n<p><strong>call functions<\/strong><br \/>\nmain(increment,1)<br \/>\nmain(decrement,1)<br \/>\no\/p<br \/>\n2<br \/>\n0<\/p>\n<p><strong>also, a function can return another function<\/strong><br \/>\n<code>def call_fun():<br \/>\ndef return_fun():<br \/>\nprint(\"yo\")<br \/>\nreturn return_fun<br \/>\nwer = call_fun()<br \/>\nwer()<br \/>\no\/p<br \/>\nYo<\/code><\/p>\n<p>In the above example, return_fun returns the call_fun when called.<\/p>\n<p>The objects which allow the __call__() method are called callable. So, a decorator is a callable object that returns a callable. In general, one can say, a decorator takes a function and adds some functionality, and returns it.<\/p>\n<p><strong>Now let\u2019s consider an e.g that shows us how to decorate a function<\/strong><br \/>\n<code>def deco(func):<br \/>\ndef inner():<br \/>\nprint(\"decorated\")<br \/>\nfunc()<br \/>\nreturn inner<br \/>\ndef initial():<br \/>\nprint(\"initial\")<br \/>\nOn run this will provide o\/p like this:<br \/>\n&gt;&gt; initial()<br \/>\ninitial<br \/>\n# on decorating initial function<br \/>\n&gt;&gt; decorate = deco(initial)<br \/>\n&gt;&gt; decorate()<br \/>\ndecorated<br \/>\ninitial<\/code><\/p>\n<p>Here, in the above example <strong>deco()<\/strong> is a decorator. The function <strong>initial()<\/strong> is decorated by function deco(). Here as one can see how deco() changes or we can say add some new functionality to the initial function. It\u2019s similar to wrapping a thing, so one can refer to a decorator as a wrapper within which some new or changed functionalities can be wrapped.<\/p>\n<p><strong><em>Note:<\/em><\/strong> the function carried out by the decorator will never gonna alter.<\/p>\n<p>That\u2019s why one can use @ symbol followed by the decorator function name and write it just above the function that one wants to decorate.<\/p>\n<p><strong>Just like <\/strong><br \/>\n<code>@deco<br \/>\ndef initial():<br \/>\nprint(\"initial\")<\/code><\/p>\n<p><strong>Now let\u2019s discuss how one can decorate a function in other ways like with parameters.<\/strong><br \/>\nLet\u2019s consider an example<br \/>\n<code>def division(m, n):<br \/>\nreturn m\/n<\/code><br \/>\nWe have two parameters m,n in the above function. Here if value 0 is passed for n then it\u2019ll throw an error.<br \/>\nSo now we&#8217;re going to decorate the <strong>division()<\/strong>.<br \/>\n<code>def deco_divide(func):<br \/>\ndef one(m, n):<br \/>\nprint(\"divide\", a, \"and\", b)<br \/>\nif n == 0:<br \/>\nprint(\"oops! Can\u2019t be divisible\")<br \/>\nreturn<br \/>\nreturn func(m, n)<br \/>\nreturn inner<br \/>\n@deco_divide<br \/>\ndef division(m, n):<br \/>\nprint(m\/n)<br \/>\n&gt;&gt; division(2,5)<br \/>\ndivide 2 and 5<br \/>\n0.4<br \/>\n&gt;&gt; division(2,0)<br \/>\ndivide 2 and 0<br \/>\noops! Can\u2019t be divisible<\/code><\/p>\n<p>One can notice that the parameters of the draw inside the inner() function decorator is the same as the parameters of functions it decorates.<br \/>\nWe can say that the decorator can work with any no of arguments. So, one can also use *agrs and **kwargs in function to decorate.<\/p>\n<p>Note: <strong>*agrs<\/strong> works as a tuple of positional arguments and *<strong>*kwargs<\/strong> works as a dictionary of positional arguments.<br \/>\n<code>def all_works(func):<br \/>\ndef inner(*args, **kwargs):<br \/>\nprint(\"decorate any function\")<br \/>\nreturn func(*args, **kwargs)<br \/>\nreturn inner<br \/>\n<\/code><\/p>\n<p><small><em>People having good knowledge of Financial accounting can get an accounting certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to <a href=\"https:\/\/www.studysection.com\/financial-accounting-advanced\">Financial Accounting<\/a> or you can go for advanced level certification if you have expert level skills in Financial Accounting.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a very interesting feature in python known as Decorators, that is used to add additional functionality to the<\/p>\n","protected":false},"author":1,"featured_media":6184,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[813,33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Decorators with Python - StudySection Blog<\/title>\n<meta name=\"description\" content=\"Python has a really interesting feature called decorators that is used to add functionality to already written code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/decorators-with-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Decorators with Python - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"Python has a really interesting feature called decorators that is used to add functionality to already written code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/decorators-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-02T05:16:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-02T05:46:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/11\/Python1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Decorators with Python\",\"datePublished\":\"2022-11-02T05:16:38+00:00\",\"dateModified\":\"2022-11-02T05:46:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/\"},\"wordCount\":521,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Decorators\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/decorators-with-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/\",\"name\":\"Decorators with Python - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-11-02T05:16:38+00:00\",\"dateModified\":\"2022-11-02T05:46:18+00:00\",\"description\":\"Python has a really interesting feature called decorators that is used to add functionality to already written code.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/decorators-with-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/decorators-with-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Decorators with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Decorators with Python - StudySection Blog","description":"Python has a really interesting feature called decorators that is used to add functionality to already written code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/decorators-with-python\/","og_locale":"en_US","og_type":"article","og_title":"Decorators with Python - StudySection Blog","og_description":"Python has a really interesting feature called decorators that is used to add functionality to already written code.","og_url":"https:\/\/studysection.com\/blog\/decorators-with-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-11-02T05:16:38+00:00","article_modified_time":"2022-11-02T05:46:18+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/11\/Python1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/decorators-with-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/decorators-with-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Decorators with Python","datePublished":"2022-11-02T05:16:38+00:00","dateModified":"2022-11-02T05:46:18+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/decorators-with-python\/"},"wordCount":521,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Decorators","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/decorators-with-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/decorators-with-python\/","url":"https:\/\/studysection.com\/blog\/decorators-with-python\/","name":"Decorators with Python - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-11-02T05:16:38+00:00","dateModified":"2022-11-02T05:46:18+00:00","description":"Python has a really interesting feature called decorators that is used to add functionality to already written code.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/decorators-with-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/decorators-with-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/decorators-with-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Decorators with Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":201,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6183"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=6183"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6183\/revisions"}],"predecessor-version":[{"id":6187,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6183\/revisions\/6187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6184"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}