<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abe Garcia</title>
    <description>The latest articles on DEV Community by Abe Garcia (@abe21412).</description>
    <link>https://dev.to/abe21412</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F164944%2Ffc0d57cd-1fcf-4d3c-a37a-9d3e61f8bdfa.png</url>
      <title>DEV Community: Abe Garcia</title>
      <link>https://dev.to/abe21412</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abe21412"/>
    <language>en</language>
    <item>
      <title>Working With Decorators In Python</title>
      <dc:creator>Abe Garcia</dc:creator>
      <pubDate>Thu, 30 May 2019 23:58:33 +0000</pubDate>
      <link>https://dev.to/abe21412/working-with-decorators-in-python-559m</link>
      <guid>https://dev.to/abe21412/working-with-decorators-in-python-559m</guid>
      <description>&lt;p&gt;In my last &lt;a href="https://dev.to/abe21412/understanding-functional-closures-4la8"&gt;post&lt;/a&gt;, I wrote about functional closures in Python and I mentioned that the most interesting thing about them is that they can be used to create decorators. And they do just that, they decorate existing functions with additional functionality, and they're also reusable so they can be applied to multiple different functions at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Decorators Defined&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A decorator is essentially a higher order function that both accepts a function as an argument and also returns a function. The result is a function that has the same functionality as the one you passed into the decorator along with whatever extra functionality you defined within the decorator body. This is best explained through a code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_func&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm a function!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I've been decorated!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;inner&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;decorated_func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;example_func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;decorated_func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we run the program, the output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I'm a function!
I've been decorated!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Decorators actually have a cool bit of syntactic sugar where instead of typing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;decorated_func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;example_func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;decorated_func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can just add &lt;code&gt;@decorator&lt;/code&gt; above the decorated function's definition like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_func&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm a function!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if you call &lt;code&gt;example_func()&lt;/code&gt;, the output will be the same as it was previously.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decorating A Function That Takes Arguments
&lt;/h2&gt;

&lt;p&gt;Decorators can also be applied to a function that takes in parameters, and the decorator will actually have access to any parameters passed into the decorated function.&lt;/p&gt;

&lt;p&gt;Take this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;capitalize_first_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;capitalized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capitalized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;capitalize_first_name&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Hi, my name is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, for example, if we say &lt;code&gt;print_name("John", "Doe")&lt;/code&gt;,&lt;br&gt;
the output will be &lt;code&gt;Hi, my name is JOHN Doe!&lt;/code&gt; without us ever having to alter the original function.&lt;/p&gt;

&lt;p&gt;And you don't always need to have the same number of arguments for a decorator to work; you can pass the &lt;code&gt;*args&lt;/code&gt; operator into the wrapper and be able to work with any function with any number of arguments!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Decorators are, in my opinion, one of the coolest features that Python has to offer. You don't always have a good reason to use them, and you probably shouldn't unless you actually have to, but they can be incredibly useful when the situation calls for them. Decorators are highly used in web frameworks like Flask and Django to add HTTP routing functionality, authentication, and more.&lt;/p&gt;

</description>
      <category>python</category>
      <category>decorators</category>
    </item>
    <item>
      <title>Understanding Functional Closures</title>
      <dc:creator>Abe Garcia</dc:creator>
      <pubDate>Thu, 09 May 2019 19:58:10 +0000</pubDate>
      <link>https://dev.to/abe21412/understanding-functional-closures-4la8</link>
      <guid>https://dev.to/abe21412/understanding-functional-closures-4la8</guid>
      <description>&lt;p&gt;While learning functional programming in JavaScript and Python, one of the hardest concepts to wrap my head around was the idea of closures. Every explanation I found only served to confuse me more so I decided to write a clear explanation with real code examples. &lt;/p&gt;

&lt;h1&gt;
  
  
  What Is A Closure?
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Inner Functions
&lt;/h2&gt;

&lt;p&gt;The first step to understanding closures is understanding inner functions, i.e., a function declared within another function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;#prints 8 to the console
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we call the function &lt;code&gt;outer()&lt;/code&gt;, we can see that it prints out 8. This is because the inner function has access to the variable &lt;code&gt;x&lt;/code&gt;, which is in its enclosing scope. So the flow here is &lt;code&gt;outer&lt;/code&gt; begins execution and sets the variable &lt;code&gt;x&lt;/code&gt; equal to three. The next thing it does is define a function within itself which will print the result of adding 5 to &lt;code&gt;x&lt;/code&gt;. It then executes the inner function and returns control to the main program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning The Inner Function
&lt;/h2&gt;

&lt;p&gt;The interesting part happens when we return the inner function from the outer function instead of calling it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;inner&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;#here we save the returned value of the outer function to a variable
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you were to then print the result variable, this is what gets printed to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;inner&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x7f5e8c381668&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Interestingly, we now have a function object assigned to our result variable, which means we can actually call it by adding parentheses to it.&lt;br&gt;
If we call our new &lt;code&gt;result()&lt;/code&gt; function, we can see that it still prints out 8. This means that our result function actually remembered what was inside its enclosing scope (the outer function) even though the outer function finished execution long before we ever called our result function. And that's exactly what a closure is! It's the recalling of a variable declared in an enclosing scope even when the program execution has moved on and is no longer in that scope. You could even use the 'del' keyword to delete the &lt;code&gt;outer&lt;/code&gt; function and everything inside it, but our &lt;code&gt;result&lt;/code&gt; function would still remember the value of &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Use A Closure?
&lt;/h1&gt;

&lt;p&gt;Using a closure can create some form of data privacy, since the main program has no access to anything declared inside the outer function. The only things that could have access to those variables are any functions defined inside the outer function itself. If you're a Python developer though, the most interesting application of closures by far is the concept of decorators.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
