{"id":769,"date":"2013-05-01T00:00:00","date_gmt":"2013-05-01T00:00:00","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2013\/05\/01\/python-decorators-sp-569390380\/"},"modified":"2021-08-01T03:57:22","modified_gmt":"2021-08-01T03:57:22","slug":"python-decorators-sp-569390380","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html","title":{"rendered":"Python – Decorators"},"content":{"rendered":"

A decorator provides a simplified way to pass one callable object to another (i.e a class or function).
\nThe main benefit of decorators is that it allows you to clearly state where and what objects are being passed to each other.<\/p>\n

Typically this can be achieved by the following.<\/p>\n

Without Decorators<\/strong><\/h3>\n

As you can see, once the functions are assigned, we pass the example function to the bold function and then assign this back to the example function object.<\/p>\n

import sys\r\n\r\n def bold(funct):\r\n\u00a0\u00a0\u00a0 print >> sys.stderr, \"adding bold tags.\"\r\n\u00a0\u00a0\u00a0 def wrapper(*args,**kwargs):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 input = str(funct(*args,**kwargs))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"<b>\"+input+\"<\/b>\"\r\n\u00a0\u00a0\u00a0 return wrapper\r\n\r\n def h3(funct):\r\n\u00a0\u00a0\u00a0 print >> sys.stderr, \"adding h3 tags.\"\r\n\u00a0\u00a0\u00a0 def wrapper(*args,**kwargs):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 input = str(funct(*args,**kwargs))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"<h3>\"+input+\"<\/h3>\"\r\n\u00a0\u00a0\u00a0 return wrapper\r\n\r\n def example(*args):\r\n\u00a0\u00a0\u00a0 print \"you entered:\",str(args[0])\r\n\u00a0\u00a0\u00a0 return args[0]\r\n\r\nexample = bold(example)\r\nexample = h3(example)<\/pre>\n

Decorators<\/strong><\/h3>\n

To perform the above but via the use of decorators the following is used. As you can see the functions are still assigned but in order to pass the functions between each other the ‘@’ character and then the function name is applied above each function.<\/p>\n

import sys\r\n\r\n def bold(funct):\r\n\u00a0\u00a0\u00a0 print >> sys.stderr, \"adding bold tags.\"\r\n\u00a0\u00a0\u00a0 def wrapper(*args,**kwargs):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 input = str(funct(*args,**kwargs))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"<b>\"+input+\"<\/b>\"\r\n\u00a0\u00a0\u00a0 return wrapper\r\n\r\n def h3(funct):\r\n\u00a0\u00a0\u00a0 print >> sys.stderr, \"adding h3 tags.\"\r\n\u00a0\u00a0\u00a0 def wrapper(*args,**kwargs):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 input = str(funct(*args,**kwargs))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"<h3>\"+input+\"<\/h3>\"\r\n\u00a0\u00a0\u00a0 return wrapper\r\n\r\n@bold\r\n@h3\r\ndef example(*args):\r\n\u00a0\u00a0\u00a0 print \"you entered:\",str(args[0])\r\n\u00a0\u00a0\u00a0 return args[0]<\/pre>\n

Note : In both instances (example within and without decorators) the following output is achieved,<\/p>\n

>>> example(1234)\r\nyou entered: 1234\r\n'<h3><b>1234<\/b><\/h3>'<\/pre>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows you to clearly state where and what objects are being passed to each other. Typically this can be achieved by the following. Without Decorators As you can see, … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"yoast_head":"\nPython - Decorators - Fir3net<\/title>\n<meta name=\"description\" content=\"A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows\" \/>\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.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Decorators - Fir3net\" \/>\n<meta property=\"og:description\" content=\"A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\" \/>\n<meta property=\"og:site_name\" content=\"Fir3net\" \/>\n<meta property=\"article:published_time\" content=\"2013-05-01T00:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-01T03:57:22+00:00\" \/>\n<meta name=\"author\" content=\"Rick Donato\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Donato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\"},\"author\":{\"name\":\"Rick Donato\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\"},\"headline\":\"Python – Decorators\",\"datePublished\":\"2013-05-01T00:00:00+00:00\",\"dateModified\":\"2021-08-01T03:57:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\"},\"wordCount\":144,\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\",\"url\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\",\"name\":\"Python - Decorators - Fir3net\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/#website\"},\"datePublished\":\"2013-05-01T00:00:00+00:00\",\"dateModified\":\"2021-08-01T03:57:22+00:00\",\"description\":\"A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows\",\"breadcrumb\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.fir3net.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming\",\"item\":\"https:\/\/www.fir3net.com\/programming\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python\",\"item\":\"https:\/\/www.fir3net.com\/programming\/python\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python – Decorators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.fir3net.com\/#website\",\"url\":\"https:\/\/www.fir3net.com\/\",\"name\":\"Fir3net\",\"description\":\"Keeping you in the know\",\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.fir3net.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.fir3net.com\/#organization\",\"name\":\"Fir3net\",\"url\":\"https:\/\/www.fir3net.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"width\":390,\"height\":88,\"caption\":\"Fir3net\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\",\"name\":\"Rick Donato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"caption\":\"Rick Donato\"},\"description\":\"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Decorators - Fir3net","description":"A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows","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.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html","og_locale":"en_US","og_type":"article","og_title":"Python - Decorators - Fir3net","og_description":"A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows","og_url":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html","og_site_name":"Fir3net","article_published_time":"2013-05-01T00:00:00+00:00","article_modified_time":"2021-08-01T03:57:22+00:00","author":"Rick Donato","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Donato","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html#article","isPartOf":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html"},"author":{"name":"Rick Donato","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037"},"headline":"Python – Decorators","datePublished":"2013-05-01T00:00:00+00:00","dateModified":"2021-08-01T03:57:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html"},"wordCount":144,"publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html","url":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html","name":"Python - Decorators - Fir3net","isPartOf":{"@id":"https:\/\/www.fir3net.com\/#website"},"datePublished":"2013-05-01T00:00:00+00:00","dateModified":"2021-08-01T03:57:22+00:00","description":"A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows","breadcrumb":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-decorators-sp-569390380.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fir3net.com\/"},{"@type":"ListItem","position":2,"name":"Programming","item":"https:\/\/www.fir3net.com\/programming"},{"@type":"ListItem","position":3,"name":"Python","item":"https:\/\/www.fir3net.com\/programming\/python"},{"@type":"ListItem","position":4,"name":"Python – Decorators"}]},{"@type":"WebSite","@id":"https:\/\/www.fir3net.com\/#website","url":"https:\/\/www.fir3net.com\/","name":"Fir3net","description":"Keeping you in the know","publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fir3net.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fir3net.com\/#organization","name":"Fir3net","url":"https:\/\/www.fir3net.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","width":390,"height":88,"caption":"Fir3net"},"image":{"@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037","name":"Rick Donato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","caption":"Rick Donato"},"description":"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders."}]}},"_links":{"self":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/769"}],"collection":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/comments?post=769"}],"version-history":[{"count":0,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/769\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media?parent=769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/categories?post=769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/tags?post=769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}