{"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
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>\nDecorators<\/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>\nNote : 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":"\n
Python - Decorators - Fir3net<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n