{"id":4056,"date":"2018-02-07T05:56:06","date_gmt":"2018-02-07T05:56:06","guid":{"rendered":"https:\/\/www.botreetechnologies.com\/blog\/?p=4056"},"modified":"2020-12-10T18:14:38","modified_gmt":"2020-12-10T12:44:38","slug":"python-decorators","status":"publish","type":"post","link":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/","title":{"rendered":"Python Decorators"},"content":{"rendered":"\n<p>Ever since I started working on\u00a0<a href=\"https:\/\/www.botreetechnologies.com\/django-development\" target=\"_blank\" rel=\"noreferrer noopener\">Django framework<\/a>, one thing that perplexed me was &#8216;decorators&#8217;. They are generally applied before method definition and also on the classes. In this article we shall be focusing on &#8216;method based decorators&#8217;.<\/p>\n\n\n\n<p>Fist let us understand some function related concepts.<\/p>\n\n\n\n<p>Functions are first-class objects. This means that functions can be passed around, and used as arguments, just like any other value (e.g, string, int, float). Decorator uses this same feature to add more value to your function.<\/p>\n\n\n\n<p>A decorator wraps your function and adds some of its own functionality before and after your function execution.<\/p>\n\n\n\n<p>Let us see an example<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/5d2c28039f94ce5639c689a12dd5e91d.js\"><\/script><br>\nmy_function&nbsp;is a normal function. For wrapping it we define&nbsp;<code>my_decorator()<\/code><\/p>\n\n\n\n<p>which takes&nbsp;my_function&nbsp;or any other function as an argument.&nbsp;<code>my_decorator()<\/code>&nbsp;has an inner function&nbsp;<code>wrapper()<\/code>&nbsp;this is the core function which wraps additional functionality around our function. The output of the above code would be as follow,<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/7e0f766c80c52ece8f2350bc45c49719.js\"><\/script><\/p>\n\n\n\n<p>To accommodate function which takes an argument we add two parameters in the wrapper function<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/91a0451ed67dd32078413701baa49a4c.js\"><\/script><\/p>\n\n\n\n<p>Adding syntactic sugar &#8216;@&#8217;<\/p>\n\n\n\n<p>Instead of wrapping the function as mentioned in the last two lines above you can also use the &#8216;@&#8217; for decorating a function as follow<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/c0bce04cc3e802c411307a6f3c7e2801.js\"><\/script><\/p>\n\n\n\n<p>But sometimes you need a decorator itself to take an argument, below is the code for such case<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/30ce52e5cd0869ac78f9d6f1503e5ee7.js\"><\/script><\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/162217ba6fc2b97ed5bca338bde6d7b2.js\"><\/script><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.botreetechnologies.com\/python-development\" target=\"_blank\" rel=\"noopener noreferrer\">Click here for more details&#8230;<\/a><\/h3>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>At <a href=\"https:\/\/www.botreetechnologies.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">BoTree Technologies<\/a>, we build enterprise applications with our Python team of 15+ engineers.<\/p>\n\n\n\n<p>We also specialize in RPA, AI, Django, JavaScript and ReactJS.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.botreetechnologies.com\/contact\" target=\"_blank\" rel=\"noopener noreferrer\">Consulting is free<\/a> &#8211; let us help you grow!<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Ever since I started working on\u00a0Django framework, one thing that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":14432,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,10],"tags":[],"class_list":["post-4056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Decorators<\/title>\n<meta name=\"description\" content=\"Ever since I started working on Django, one thing that perplexed me was \u2018decorators\u2019. They are generally applied before method definition and also on the classes. In this article we shall be focusing on \u2018method based decorators\u2019.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Decorators\" \/>\n<meta property=\"og:description\" content=\"Ever since I started working on Django, one thing that perplexed me was \u2018decorators\u2019. They are generally applied before method definition and also on the classes. In this article we shall be focusing on \u2018method based decorators\u2019.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\" \/>\n<meta property=\"og:site_name\" content=\"BoTree Technologies\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BoTreeTechnologies\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-07T05:56:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-10T12:44:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"852\" \/>\n\t<meta property=\"og:image:height\" content=\"479\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"BoTree Technologies\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:site\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"BoTree Technologies\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\"},\"author\":{\"name\":\"BoTree Technologies\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6b173b8b34382c4bf676d3bd528467fd\"},\"headline\":\"Python Decorators\",\"datePublished\":\"2018-02-07T05:56:06+00:00\",\"dateModified\":\"2020-12-10T12:44:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\"},\"wordCount\":263,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg\",\"articleSection\":[\"Python\",\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\",\"name\":\"Python Decorators\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg\",\"datePublished\":\"2018-02-07T05:56:06+00:00\",\"dateModified\":\"2020-12-10T12:44:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6b173b8b34382c4bf676d3bd528467fd\"},\"description\":\"Ever since I started working on Django, one thing that perplexed me was \u2018decorators\u2019. They are generally applied before method definition and also on the classes. In this article we shall be focusing on \u2018method based decorators\u2019.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg\",\"width\":852,\"height\":479,\"caption\":\"Python Decorators\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.botreetechnologies.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Decorators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/\",\"name\":\"BoTree Technologies\",\"description\":\"Committed to inspire generation.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6b173b8b34382c4bf676d3bd528467fd\",\"name\":\"BoTree Technologies\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/admin-icon-150x150.png\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/admin-icon-150x150.png\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/admin-icon-150x150.png\",\"caption\":\"BoTree Technologies\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Decorators","description":"Ever since I started working on Django, one thing that perplexed me was \u2018decorators\u2019. They are generally applied before method definition and also on the classes. In this article we shall be focusing on \u2018method based decorators\u2019.","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:\/\/www.botreetechnologies.com\/blog\/python-decorators\/","og_locale":"en_US","og_type":"article","og_title":"Python Decorators","og_description":"Ever since I started working on Django, one thing that perplexed me was \u2018decorators\u2019. They are generally applied before method definition and also on the classes. In this article we shall be focusing on \u2018method based decorators\u2019.","og_url":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/","og_site_name":"BoTree Technologies","article_publisher":"https:\/\/www.facebook.com\/BoTreeTechnologies\/","article_published_time":"2018-02-07T05:56:06+00:00","article_modified_time":"2020-12-10T12:44:38+00:00","og_image":[{"width":852,"height":479,"url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg","type":"image\/jpeg"}],"author":"BoTree Technologies","twitter_card":"summary_large_image","twitter_creator":"@BoTreeTech","twitter_site":"@BoTreeTech","twitter_misc":{"Written by":"BoTree Technologies","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#article","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/"},"author":{"name":"BoTree Technologies","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6b173b8b34382c4bf676d3bd528467fd"},"headline":"Python Decorators","datePublished":"2018-02-07T05:56:06+00:00","dateModified":"2020-12-10T12:44:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/"},"wordCount":263,"commentCount":0,"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg","articleSection":["Python","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/","url":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/","name":"Python Decorators","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage"},"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg","datePublished":"2018-02-07T05:56:06+00:00","dateModified":"2020-12-10T12:44:38+00:00","author":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6b173b8b34382c4bf676d3bd528467fd"},"description":"Ever since I started working on Django, one thing that perplexed me was \u2018decorators\u2019. They are generally applied before method definition and also on the classes. In this article we shall be focusing on \u2018method based decorators\u2019.","breadcrumb":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#primaryimage","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/python-decorators.jpg","width":852,"height":479,"caption":"Python Decorators"},{"@type":"BreadcrumbList","@id":"https:\/\/www.botreetechnologies.com\/blog\/python-decorators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.botreetechnologies.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Decorators"}]},{"@type":"WebSite","@id":"https:\/\/www.botreetechnologies.com\/blog\/#website","url":"https:\/\/www.botreetechnologies.com\/blog\/","name":"BoTree Technologies","description":"Committed to inspire generation.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6b173b8b34382c4bf676d3bd528467fd","name":"BoTree Technologies","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/admin-icon-150x150.png","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/admin-icon-150x150.png","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/admin-icon-150x150.png","caption":"BoTree Technologies"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/4056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=4056"}],"version-history":[{"count":1,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/4056\/revisions"}],"predecessor-version":[{"id":14433,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/4056\/revisions\/14433"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media\/14432"}],"wp:attachment":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=4056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=4056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=4056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}