{"id":1919,"date":"2019-06-14T13:24:26","date_gmt":"2019-06-14T13:24:26","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1919"},"modified":"2019-06-14T17:18:19","modified_gmt":"2019-06-14T17:18:19","slug":"section-12-python-generators","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1919","title":{"rendered":"Section 12: Python Decorators"},"content":{"rendered":"<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1915\">&lt; Section 11<\/a> | Section 12 &gt;<\/p>\n<h1>Decorators with Python Overview<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9516654#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497658#questions<\/a><br \/>\n<b>There is A LOT going on here, so pay attention to the steps in order!<\/b><\/p>\n<h2>Decorators<\/h2>\n<p>Decorators allow you to &#8216;decorate&#8217; or &#8216;wrap&#8217; a function with additional code.<\/p>\n<pre>def simple_func():\r\n    # do some stuff\r\n    return something<\/pre>\n<p>Later you might want to add some new functionality to that existing function:<\/p>\n<pre>def simple_func():\r\n    # do some new stuff\r\n    # do some stuff\r\n    return something<\/pre>\n<ul>\n<li>You can add the extra code (functionality) to the old function<\/li>\n<li>You can create a brand new function that contains the old code, and then add the new code to it<\/li>\n<\/ul>\n<p>What if you want to remove the new functionality at a later date?<\/p>\n<ul>\n<li>You would need to delete it manually or not call the function<\/li>\n<\/ul>\n<p>Python decorators allow you to tack on extra functionality to an already existing function.\u00a0 Decorators use the &#8216;@&#8217; operator and are then placed on the top of the original function<\/p>\n<pre>@some_decorator\r\ndef simple_func():\r\n    # do some stuff\r\n    return something<\/pre>\n<p>Example:<\/p>\n<pre>def hello():\r\n    print(\"Hello!\")\r\nhello<\/pre>\n<p>&lt;function __main__.hello()&gt;<\/p>\n<pre>hello()<\/pre>\n<p>Hello!<\/p>\n<p>Now create a &#8216;copy&#8217; of hello by assigning it to &#8216;greet()&#8217;<\/p>\n<pre>greet = hello\r\ngreet<\/pre>\n<p>&lt;function __main__.hello()&gt;<\/p>\n<pre>greet()<\/pre>\n<p>Hello!<\/p>\n<p>Now prove it&#8217;s pointing to an object and not pointing to hello by deleting hello()<\/p>\n<pre>del hello\r\nhello()<\/pre>\n<p>NameError: name &#8216;hello&#8217; is not defined<\/p>\n<p>Prove greet still works<\/p>\n<pre>greet()<\/pre>\n<p>Hello!<\/p>\n<pre>greet<\/pre>\n<p>&lt;function __main__.hello()&gt;<\/p>\n<h2>Creating functions within functions and passing them<\/h2>\n<pre>def hello(name='Sofia'):\r\n    print(\"You are in function hello()\")\r\n    def lastname():\r\n        return 'Roberts'\r\n    def another_name():\r\n        return 'is not a Roberts'\r\n    print(lastname())\r\n    print(another_name())\r\n    print(\"I will return a function depending on your name.\")\r\n    if name == 'Sofia':\r\n        return lastname\r\n    else:\r\n        return another_name\r\niam = hello('Sofia')\r\nprint(iam())<\/pre>\n<p>You are in function hello()<br \/>\nRoberts<br \/>\nis not a Roberts<br \/>\nI will return a function depending on your name.<br \/>\nRoberts<\/p>\n<p>Same thing, short ans sweet.<\/p>\n<pre>def cool():\r\n    def super_cool():\r\n        return \"I am super cool!\"\r\n    return super_cool<\/pre>\n<pre>whoami=cool()<\/pre>\n<p>&#8212; no output, just assigned whoami to super_cool &#8212;<\/p>\n<pre>print(whoami())<\/pre>\n<p>I am super cool!<\/p>\n<h2>Passing a function as an argument<\/h2>\n<p>Super basic<\/p>\n<pre>def hello():\r\n    print(\"Hello!\")\r\ndef goodbye():\r\n    print(\"Goodbye!\")\r\ndef other(some_defined_function):\r\n    some_defined_function()\r\n    # print the function location for good measure\r\n    print(some_defined_function)\r\nother(hello)\r\nother(goodbye)<\/pre>\n<p>Hello!<\/p>\n<p>Goodbye!<\/p>\n<h2>Create a Decorator<\/h2>\n<pre>def new_decorator(original_func):\r\n    '''\r\n    accepts an existing function and wraps it with additional code\r\n    '''\r\n    def wrap_func():\r\n        #add some optional starting code\r\n        print(\"Here is my new song, I hope you like it!\")\r\n        # now run the original function\r\n        original_func()\r\n        #now add any remaining code\r\n        print(\"But some day I'm going to be an object!\")\r\n    # return the 'new script'\r\n    return wrap_func\r\ndef my_original_function():\r\n    print(\"I'm just an old piece of code.\")\r\n\r\n\r\n#assign a new function name to the 'new' code\r\ndecorated = new_decorator(my_original_function)\r\n\r\n# run the new function\r\ndecorated()<\/pre>\n<p>Here is my new song, I hope you like it!<br \/>\nI&#8217;m just an old piece of code.<br \/>\nBut some day I&#8217;m going to be an object!<\/p>\n<p>Now that we understand how they work, here is the shortcut version!<\/p>\n<pre>def new_decorator(original_func):\r\n    '''\r\n    accepts an existing function and wraps it with additional code\r\n    '''\r\n    def wrap_func():\r\n        #add some optional starting code\r\n        print(\"Here is my new song, I hope you like it!\")\r\n        # now run the original function\r\n        original_func()\r\n        #now add any remaining code\r\n        print(\"But some day I'm going to be an object!\")\r\n    # return the 'new script'\r\n    return wrap_func\r\n\r\n@new_decorator\r\ndef my_original_function():\r\n    print(\"I'm just an old piece of code.\")\r\n\r\n# Run the original function\r\nmy_original_function()<\/pre>\n<p>Here is my new song, I hope you like it!<br \/>\nI&#8217;m just an old piece of code.<br \/>\nBut some day I&#8217;m going to be an object!<\/p>\n<h1>Decorators Homework<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3767592#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3767592#questions<\/a><\/p>\n<p>Flask: <a href=\"http:\/\/flask.pocoo.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/flask.pocoo.org\/<\/a><\/p>\n<ul>\n<li>Good for creating web pages with Python<\/li>\n<\/ul>\n<p>Django:\u00a0<a href=\"https:\/\/www.djangoproject.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.djangoproject.com\/<\/a><\/p>\n<ul>\n<li>A very popular web framework<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt; Section 11 | Section 12 &gt; Decorators with Python Overview https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497658#questions There is A LOT going on here, so pay attention to the steps in order! Decorators Decorators allow you to &#8216;decorate&#8217; or &#8216;wrap&#8217; a function with additional code. def simple_func(): # do some stuff return something Later you might want to add some ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1919\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1919","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1919"}],"version-history":[{"count":3,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1919\/revisions"}],"predecessor-version":[{"id":1924,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1919\/revisions\/1924"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}