<?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: Snehangsu De</title>
    <description>The latest articles on DEV Community by Snehangsu De (@snehangsude).</description>
    <link>https://dev.to/snehangsude</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F723412%2F76ed4dfe-ebe3-4f24-bb58-2cb4b3cdb042.png</url>
      <title>DEV Community: Snehangsu De</title>
      <link>https://dev.to/snehangsude</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/snehangsude"/>
    <language>en</language>
    <item>
      <title>The Brotherhood of : `__str__` &amp; `__repr__`</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Wed, 09 Mar 2022 13:25:08 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-brotherhood-of-str-repr-132f</link>
      <guid>https://dev.to/snehangsude/the-brotherhood-of-str-repr-132f</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;If you want to view the notebook version of this, you can view it &lt;a href="https://snehangsude.github.io/xSpace/python/dunder_method/oop/2022/03/03/first.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Diving in the nitty-gritty details of Python introduces us to Dunder Methods. &lt;strong&gt;Dunder&lt;/strong&gt; means &lt;strong&gt;&lt;em&gt;"double underscores"&lt;/em&gt;&lt;/strong&gt; as it starts and ends with two underscores. A dunder method acts as a contract between the Python interpreter and the person who made a particular Python class. &lt;/p&gt;

&lt;p&gt;For example, the &lt;code&gt;len&lt;/code&gt; method, it helps us with the length of any given string or list or a dictionary, however, it uses a dunder method &lt;code&gt;__len__&lt;/code&gt;. Since the length of something is fundamental to any programming language it was made a built-in method but nonetheless, it internally delegates the task to &lt;code&gt;__len__&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; integers = [1, 2, 3, 4, 5]
&amp;gt;&amp;gt;&amp;gt; len(integers)
Output: 5

&amp;gt;&amp;gt;&amp;gt; integers.__len__()
Output: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are quite a few built-in functions that internally use dunder methods to work, such as indexing which uses &lt;code&gt;__getitem__&lt;/code&gt;, or print which uses &lt;code&gt;__str__&lt;/code&gt;. If you are interested to know about all dunder methods here's a good resource: &lt;a href="https://docs.python.org/3/reference/datamodel.html#special-method-names"&gt;Dunder Methods&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Difference
&lt;/h2&gt;

&lt;p&gt;Understanding &lt;code&gt;__str__&lt;/code&gt; &amp;amp; &lt;code&gt;__repr__&lt;/code&gt; allows us greater control for us to not only edit and upgrade classes but also to debug and create libraries. &lt;br&gt;
In a nutshell, if I had to explain to you about &lt;code&gt;__str__&lt;/code&gt; or &lt;code&gt;__repr__&lt;/code&gt; they both do the same thing i.e. they print an output. &lt;strong&gt;However, &lt;code&gt;__repr__&lt;/code&gt; is mostly used by programmers to debug, and &lt;code&gt;__str__&lt;/code&gt; is for the users using the program.&lt;/strong&gt; Let's dive in to see how exactly are they both different and learn about when to use which!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; repr('Triceratops')
Output: "'Triceratops'"

&amp;gt;&amp;gt;&amp;gt; str('Triceratops')
Output: 'Triceratops'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the above lines of code, there isn't much difference apart from the fact that &lt;code&gt;repr('Triceratops')&lt;/code&gt; throws us an output which is within a double-inverted comma (&lt;code&gt;""&lt;/code&gt;) and then the string within a single-inverted (&lt;code&gt;''&lt;/code&gt;) comma. As for the &lt;code&gt;str('Triceratops')&lt;/code&gt; it's within a single-inverted comma(&lt;code&gt;''&lt;/code&gt;). If you ask me, that's not even a difference as both of them do the same exact thing and there isn't much difference. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's look at something a bit more interesting.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import datetime

&amp;gt;&amp;gt;&amp;gt; current_time = datetime.datetime.now()
&amp;gt;&amp;gt;&amp;gt; print(current_time)
Output: 2022-03-03 01:04:19.955451

&amp;gt;&amp;gt;&amp;gt; str(current_time)
Output: 2022-03-03 01:04:19.955451

&amp;gt;&amp;gt;&amp;gt; repr(current_time)
Output: 'datetime.datetime(2022, 3, 3, 1, 4, 19, 955451)'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fascinating, isn't it!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While using the &lt;code&gt;datetime&lt;/code&gt; library we created a variable &lt;code&gt;current_time&lt;/code&gt; which stores as the variable says, the current time. Running a &lt;code&gt;print&lt;/code&gt; statement gives us an output that is similar to the output when we use the &lt;code&gt;current_time&lt;/code&gt;  variable inside the &lt;code&gt;str()&lt;/code&gt; function. A user-friendly output of the variable giving is the   &lt;code&gt;year-month-date hour:minutes:seconds&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, using the same variable inside the &lt;code&gt;repr()&lt;/code&gt; function gives us an entirely different output. It tells us that it's a &lt;code&gt;datetime&lt;/code&gt; object which holds the values &lt;code&gt;(year, month, day, hour, minutes, seconds, microseconds)&lt;/code&gt;. A developer-friendly output that provides adequate information for a developer to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Brotherhood
&lt;/h2&gt;

&lt;p&gt;While it may be intriguing to prefer one over another, truth be told, we need both of them to make sure they cover both cases. The general idea is to make the &lt;code&gt;__repr__&lt;/code&gt; function help you as much as you can with maximum details. While the &lt;code&gt;__str__&lt;/code&gt; function with the minimum details which a user can read or understand. For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining a class:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; class Dinosaur:
    ... def __init__(self, name):
        ... self.name = name

&amp;gt;&amp;gt;&amp;gt; def __repr__(self):
    ... return (f'{self.name.__class__.__name__}({self.name})') 

&amp;gt;&amp;gt;&amp;gt; def __str__(self):
        ... return (f'{self.name}')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; dino = Dinosaur(1)
&amp;gt;&amp;gt;&amp;gt; print(dino)
Output: 1

&amp;gt;&amp;gt;&amp;gt; str(dino)
Output: 1

&amp;gt;&amp;gt;&amp;gt; repr(dino)
Output: int(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: &lt;code&gt;__class__&lt;/code&gt; and &lt;code&gt;__name__&lt;/code&gt; represents the name of the class the &lt;code&gt;name&lt;/code&gt; belongs to.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Now that, you have got a grasp into what &lt;code&gt;__repr__&lt;/code&gt; &amp;amp; &lt;code&gt;__str__&lt;/code&gt; does, you are one step closer to debugging your own code. Try creating a class that gives you all the information using &lt;code&gt;repr()&lt;/code&gt; but only the necessary one using &lt;code&gt;str()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you are interested to know about how &lt;code&gt;datetime&lt;/code&gt; wrote their library &lt;code&gt;__repr__&lt;/code&gt; and &lt;code&gt;__str__&lt;/code&gt; function, you can see the source code &lt;a href="https://github.com/python/cpython/blob/3.10/Lib/datetime.py"&gt;here&lt;/a&gt;. It might be overwhelming when you look at the code but try to search through the document to find &lt;code&gt;__str__&lt;/code&gt; and &lt;code&gt;__repr__&lt;/code&gt;. &lt;strong&gt;Hint&lt;/strong&gt;: &lt;em&gt;Use Ctrl + F&lt;/em&gt;. This will give you a broader idea of how these functions are different, yet so similar. &lt;/p&gt;

&lt;p&gt;Oh! And something interesting while I was working on this article, I saw the Jupyter notebooks use &lt;code&gt;__repr__&lt;/code&gt; when the output is requested without the &lt;code&gt;print&lt;/code&gt;. If you want to view the notebook version of this you can use view it &lt;a href="https://snehangsude.github.io/xSpace/python/dunder_method/oop/2022/03/03/first.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Power of Inheritance</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Mon, 28 Feb 2022 14:52:42 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-power-of-inheritance-2lel</link>
      <guid>https://dev.to/snehangsude/the-power-of-inheritance-2lel</guid>
      <description>&lt;p&gt;If you Google "&lt;strong&gt;&lt;em&gt;Inheritance&lt;/em&gt;&lt;/strong&gt;" the most expected definition in the language of Biology would be that it's a trait passed down from your ancestors to you or from you to your descendants.&lt;/p&gt;

&lt;p&gt;Me, you, alike have had some specific traits or qualities that have been passed down from our ancestors. While true, this doesn't mean I or you are exactly the same as our ancestors i.e. we have our own qualities and traits in addition to the traits from them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance in Machines
&lt;/h2&gt;

&lt;p&gt;Inheritance is similar in the life of machines. It helps them take in a specific trait or functionality from a Parent Class but also add a few functionalities of its own. If you are to ask me, Inheritance is undoubtedly one of the most powerful features in &lt;em&gt;Object-Oriented&lt;/em&gt; Programming.&lt;/p&gt;

&lt;p&gt;It allows us to create an entirely new Class with no modification or changes to the Parent Class. &lt;strong&gt;The new class is called the Child Class and the class it inherits from is the Parent Class.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cd1cfMFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AX81kwvCVJN6Ls0hv" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cd1cfMFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AX81kwvCVJN6Ls0hv" width="600" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image, we have two Classes &lt;code&gt;Animal&lt;/code&gt;, which is the Parent Class, and &lt;code&gt;Dog&lt;/code&gt;, which is the Child Class. We have talked about how to define classes before and that stays exactly the same for the Parent Class. However, we have two new things which are on &lt;em&gt;Line 9&lt;/em&gt; and &lt;em&gt;Line 11&lt;/em&gt;. Let's dive in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 9:&lt;/strong&gt; To define a Child Class &lt;code&gt;Dog&lt;/code&gt;, which would inherit all the functionality from the Parent class &lt;code&gt;Animal&lt;/code&gt; we declare the Parent Class name within parenthesis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 11:&lt;/strong&gt; Now that we have defined the Parent Class we want to inherit from we would need to specify an in-built function that allows us to inherit the functionality to the Child Class. &lt;code&gt;super().__init__()&lt;/code&gt; does the exact same thing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We will see how this works as we walk through an example below&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Everything else stays the same. The Parent Class has a method &lt;code&gt;breathe&lt;/code&gt; while the Child Class has its own method &lt;code&gt;bark&lt;/code&gt;. Also, the Child Class takes in one positional argument which is the &lt;code&gt;breed&lt;/code&gt; of the Dog.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's now look at how to use these in our code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iCsXnLE7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A_fkW5khDWvzq5OmP" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iCsXnLE7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A_fkW5khDWvzq5OmP" width="600" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The initial surprise that came to me when I was learning Inheritance was that I didn't really need the Parent Class to work with the Child Class, which is such a beautiful thing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 1:&lt;/strong&gt; We have defined two Classes before &lt;code&gt;Animal&lt;/code&gt; and &lt;code&gt;Dog&lt;/code&gt;. Surprise, surprise, we don't need the &lt;code&gt;Animal&lt;/code&gt; Class to work with our &lt;code&gt;Dog&lt;/code&gt; Class though it actually inherits from the &lt;code&gt;Animal&lt;/code&gt; Class. &lt;strong&gt;&lt;em&gt;Less Code, More Functionality.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 3:&lt;/strong&gt; We have created an object &lt;code&gt;roxy&lt;/code&gt; with our class Dog passing in one positional argument &lt;code&gt;'Border Collie'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 5–8:&lt;/strong&gt; These are the attributes of the Child Class i.e. &lt;code&gt;Dog&lt;/code&gt;. Tapping into the methods and attributes provides the same output that we defined inside the Class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 11–14:&lt;/strong&gt; These are the attributes of the Parent Class i.e. &lt;code&gt;Animal&lt;/code&gt;. This is the interesting part, &lt;code&gt;Dog&lt;/code&gt; being a Child Class inherits all the attributes of the Parent Class without being explicitly defined. This is the work of the &lt;code&gt;super()&lt;/code&gt; function which has the ability to reference the Parent Class. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Without the &lt;code&gt;super()&lt;/code&gt; function trying to work with &lt;strong&gt;Line 11 -14&lt;/strong&gt; would result in an &lt;code&gt;Attribute Error&lt;/code&gt; as it would not be inheriting any attribute of Parent Class. However, the method &lt;code&gt;breathe()&lt;/code&gt; will still be inherited.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Multiple Inheritance
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ckT_KH0W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A1fzZQjL98h-oeUAw" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ckT_KH0W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A1fzZQjL98h-oeUAw" width="600" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inheritance can go into depth as well, just like us humans can inherit genes from our great grandparents. However, the important thing to remember is that the entire understanding of Inheritance stays the same i.e. &lt;strong&gt;it inherits functionality from the Parent Class.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the above image, I haven't written the demo code for the classes but the structure of the Class is shown.&lt;/p&gt;

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

&lt;p&gt;We have now embraced the power of Inheritance and why it makes our life even easier. All of this while reducing redundant lines of code and making our code re-useable. I hope you now have a good grasp of Inheritance. I look forward to seeing more of this functionality used in projects, do comment below if you have any interesting projects or in general comments or feedback.&lt;/p&gt;

&lt;p&gt;I hope this message finds you in good health! If you have any interesting suggestions or feedback, feel free to connect with me on &lt;a href="https://twitter.com/_Perceptron_"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Secrets of Self</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Sat, 05 Feb 2022 15:26:12 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-secrets-of-self-1j06</link>
      <guid>https://dev.to/snehangsude/the-secrets-of-self-1j06</guid>
      <description>&lt;p&gt;in the previous article, we learned how Object-Oriented Programming can be replicated multiple times or can be reused later for fulfilling different utilities. For example, we can import a library (a well-documented pre-written code) into our program and create objects from there to use them inside our code.&lt;/p&gt;

&lt;p&gt;While we were learning about Object-Oriented Programming, we saw the repetitive pattern for a parameter inside the Class in general as &lt;code&gt;self&lt;/code&gt;. &lt;strong&gt;Let’s dive a bit into understanding, how &lt;code&gt;self&lt;/code&gt; works and why do we use it?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need &lt;code&gt;self&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Let’s take a look at the Class &lt;code&gt;Animal&lt;/code&gt;. We have explicitly defined &lt;code&gt;self&lt;/code&gt; while introducing attributes to the class as well as while we are defining a method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZUf46bBT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/597/550/mail/main.py.png%3F1637788167" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZUf46bBT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/597/550/mail/main.py.png%3F1637788167" width="600" height="602"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We by now know that a Class creates a blueprint for us to create an Object. In the above picture, we have created two Objects named &lt;code&gt;dog1&lt;/code&gt; and &lt;code&gt;dog2&lt;/code&gt;. &lt;strong&gt;The &lt;code&gt;self&lt;/code&gt; term helps us represent the respective instance of an Object&lt;/strong&gt;. &lt;br&gt;&lt;br&gt;
&lt;em&gt;What do I mean by that?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this picture, we have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dog1&lt;/code&gt; : &lt;code&gt;name = Roxy&lt;/code&gt; &amp;amp; &lt;code&gt;breed = BorderCollie&lt;/code&gt; &lt;br&gt;&lt;br&gt;
&lt;code&gt;dog2&lt;/code&gt; : &lt;code&gt;name = Brent&lt;/code&gt; &amp;amp; &lt;code&gt;breed = Doberman&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both the Objects are created from the same class i.e. from the same blueprint, the &lt;code&gt;self&lt;/code&gt; parameter helps identify each object we have created and assign respective attributes and methods to it. &lt;br&gt;&lt;br&gt;
&lt;code&gt;dog1&lt;/code&gt; holds the name Roxy and breed as BorderCollie whereas &lt;code&gt;dog2&lt;/code&gt; holds the name Brent and the breed as Doberman. If we didn’t have the self parameter these would have not been possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  How &lt;code&gt;self&lt;/code&gt; works?
&lt;/h2&gt;

&lt;p&gt;I mentioned a hint of how &lt;code&gt;self&lt;/code&gt; works in the previous article but let’s dive into understanding what exactly happens under the hood. &lt;br&gt;&lt;br&gt;
For anyone, looking at the method &lt;code&gt;breathe()&lt;/code&gt; would seem really weird that it takes one argument however when we call it, we don’t actually need to pass an argument for it to work nor does Python tell us anything about it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4Sxf4Efa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/598/277/mail/main.py__281_29.png%3F1637791134" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4Sxf4Efa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/598/277/mail/main.py__281_29.png%3F1637791134" width="600" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we run the above commands in the file &lt;code&gt;type.py&lt;/code&gt; we see that &lt;code&gt;Animal.breathe&lt;/code&gt; is a function while &lt;code&gt;dog1.breathe&lt;/code&gt; is a method and that is quite interesting. &lt;em&gt;Why?&lt;/em&gt; Well, the thing with Python method is that it passes the object as the first parameter inside the function. &lt;br&gt;&lt;br&gt;
So, when I type in &lt;code&gt;dog1.breathe()&lt;/code&gt; into my console, it internally passes &lt;code&gt;Animal.breathe(dog1)&lt;/code&gt; as a function with the self parament being the object we create itself. &lt;/p&gt;

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

&lt;p&gt;Before we end, a quick notion that the &lt;code&gt;self&lt;/code&gt; term doesn’t really need to be &lt;code&gt;self&lt;/code&gt;, you can put it as &lt;code&gt;dinosaur&lt;/code&gt; and it would still work the same, just make sure you replace all of them with &lt;code&gt;dinosaur&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;However, it’s generally recommended that you use &lt;code&gt;self&lt;/code&gt;, as that’s followed by the entire community.&lt;br&gt;
Additionally, if you are interested you can read more on the &lt;code&gt;self&lt;/code&gt; parameter from the creator of Python itself &lt;a href="http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html?utm_campaign=xSpace&amp;amp;utm_medium=email&amp;amp;utm_source=Revue%20newsletter"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope I was able to explain why we need &lt;code&gt;self&lt;/code&gt; and how it works under the hood. If you have any interesting suggestions or feedback, feel free to connect with me on &lt;a href="https://twitter.com/_Perceptron_"&gt;Twitter&lt;/a&gt;. I also have a newsletter, which I send out every week. You can subscribe to it &lt;a href="https://www.getrevue.co/profile/xSpace/"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Elegance of Classes</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Fri, 10 Dec 2021 09:14:17 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-elegance-of-classes-3cd5</link>
      <guid>https://dev.to/snehangsude/the-elegance-of-classes-3cd5</guid>
      <description>&lt;h2&gt;
  
  
  Section 4: Objects and Classes
&lt;/h2&gt;

&lt;p&gt;If you are someone who used to code back in the late 1960s, then you know how hard writing pages and pages of code were. Today, we are effectively using &lt;em&gt;object-oriented&lt;/em&gt; programming and our lives have become easier. Back in those days, &lt;em&gt;procedure-oriented&lt;/em&gt; programming was used which focused mostly on functions, whereas &lt;em&gt;object-oriented&lt;/em&gt; programming focuses on objects.&lt;/p&gt;

&lt;p&gt;Let’s dive into understanding what objects actually are and how to create them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Objects and Classes
&lt;/h3&gt;

&lt;p&gt;To understand better, let’s take an example of a virtual zoo. Say, we want to create a virtual zoo, wherein in total there would be 2 dogs, and 1 dinosaur &lt;em&gt;(you knew Dinosaurs were coming)&lt;/em&gt;, and each with a unique nickname.&lt;/p&gt;

&lt;p&gt;To create this database using &lt;em&gt;procedure-oriented&lt;/em&gt; programming, it would not only require lists and loops to create them but as hundreds and thousands of various kinds of animals are brought in, the code would not only be lengthy but extremely hard to debug in case of any errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enter &lt;em&gt;Object-oriented&lt;/em&gt; programming.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;object-oriented&lt;/em&gt; programming, we create a blueprint defined as a class, so that we can create multiple objects using it. This is best explained in an example, so here’s how to define a class:&lt;/p&gt;

&lt;h4&gt;
  
  
  Class
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--stLJTKGU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/448/203/mail/mammal1.PNG%3F1637176713" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--stLJTKGU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/448/203/mail/mammal1.PNG%3F1637176713" width="600" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, there are a few things to put your eye on. Let’s look at them all.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Class&lt;/strong&gt;: To define a Class, we use the &lt;code&gt;class&lt;/code&gt; keyword and initialize it with the name of the class. Note: The name is in Camel-case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Constructor&lt;/strong&gt;: &lt;code&gt;init&lt;/code&gt; is a dunder method, a special kind of function. We will discuss dunder methods later but what it basically does is help us construct any &lt;strong&gt;attributes&lt;/strong&gt; of the Class hence called a constructor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Attributes&lt;/strong&gt;: So what exactly are attributes? It’s a fancy term used for variables inside the Class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Method&lt;/strong&gt;: Method is another fancy term used for Functions inside a Class. It’s generally a good idea to know the terms as they are widely used but none of the basic idea changes just cause they are inside a Class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;self&lt;/strong&gt;: When I got first introduced to object-oriented programming I had a tough time understanding what self does. I will be explaining in detail in the next blogpost but for now, accept it as something that allows the object; we will create to access both the attribute and the method inside a class. &lt;em&gt;It will become a bit more clear when we understand Objects&lt;/em&gt;. 👇🏼&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Objects
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pny2vYnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/450/521/mail/main.py__28Outputs_in_console_29__281_29.png%3F1637183731" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pny2vYnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/450/521/mail/main.py__28Outputs_in_console_29__281_29.png%3F1637183731" width="600" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Try &lt;strong&gt;Right-Clicking&lt;/strong&gt; on the image, then select &lt;strong&gt;Open image in a new tab&lt;/strong&gt; if the image is unreadable.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After declaring the Class &lt;em&gt;(previous image)&lt;/em&gt; with the file named &lt;code&gt;mammal.py&lt;/code&gt; we import the Animal Class. It’s not necessary but recommended as separating your code into two different files makes debugging easier. Once imported we create the object, let’s dive into the code and how it works internally.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creating Objects&lt;/strong&gt;: We create two objects of name &lt;code&gt;dog1&lt;/code&gt; and &lt;code&gt;dog2&lt;/code&gt; using the Animal class, just by assigning two different variables to store the Object into. I have used keyword arguments to declare the values but can be done using positional arguments too.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;What exactly happens under the hood is when I declare the Animal class and pass on the name &amp;amp; the breed it constructs the Object based on the blueprint of the Animal Class which is defined by the &lt;code&gt;init&lt;/code&gt; method. Here the &lt;code&gt;self&lt;/code&gt; word helps in connecting the arguments declared while creating the Object in &lt;code&gt;main.py&lt;/code&gt; with the attributes inside the Class Animal in &lt;code&gt;mammal.py&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Calling the Method&lt;/strong&gt;: Calling the &lt;code&gt;breathe()&lt;/code&gt; method on each of the variables inside the &lt;code&gt;main.py&lt;/code&gt; would call the Function inside the Animal Class and do whatever is defined inside the Function. Here’s it to print &lt;code&gt;'Inhale - Exhale'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;What exactly happens under the hood is when the method is called on the Object created, it passes the Object (dog1, dog2, or dinosaur1) on the &lt;code&gt;self&lt;/code&gt; parameter and then calls the Function to do the action defined inside the Function. This means that &lt;code&gt;dog1.breathe()&lt;/code&gt; translates to &lt;code&gt;Animal.breathe(dog1)&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: While calling a Method, not providing the parenthesis would not run the code instead the location the Object is created will be printed&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Changing the Attributes&lt;/strong&gt;: You may have noticed that I have changed the value for &lt;code&gt;tail&lt;/code&gt; for the Object &lt;code&gt;dog2&lt;/code&gt; as &lt;code&gt;False&lt;/code&gt;. One of the blessings of using object-oriented programming or OOP is that you can change and create variables on the go.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;dog2.tail = False&lt;/code&gt; is an example of changing the value. &lt;code&gt;dinosaur1.horn = True&lt;/code&gt; is an example of adding a new attribute to the Object.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;We have now learned the essence of Classes and why it makes our life easier without having to write redundant lines of code. Object-Oriented Programming is the way to go for us. While we have only scratched the surface of OOP, a lot more is there to unveil. And I will be talking about them in the upcoming blogposts, making it easier for you to grasp.&lt;/p&gt;

&lt;p&gt;If you have any interesting suggestions or feedbacks, feel free to connect with me on &lt;a href="https://twitter.com/__xSpace"&gt;Twitter&lt;/a&gt;. I also have a newsletter, which I send out every week Thursday. You can subscribe it &lt;a href="https://www.getrevue.co/profile/xSpace/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope you enjoy this, see you next week! 👋🏼&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Overcoming the Initial Challenge</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Fri, 03 Dec 2021 08:49:37 +0000</pubDate>
      <link>https://dev.to/snehangsude/overcoming-the-initial-challenge-4j4n</link>
      <guid>https://dev.to/snehangsude/overcoming-the-initial-challenge-4j4n</guid>
      <description>&lt;h2&gt;
  
  
  The Power of being present
&lt;/h2&gt;

&lt;p&gt;We have all started something we didn't continue. If you are above the age of 10, I'm sure you know exactly what I'm talking about. Starting from going to the gym, learning a new language, or meditation, we have all wanted to be constant. &lt;strong&gt;So, the question arises why can't we continue? Why do we fail if we want something?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine yourself deciding to start learning a new language one hour each day. You complete Day 1, Day 2, and Day 3. On Day 4, however during your schedule, an important emergency drops in and you make adjustments to your routine to attend to that emergency. Now here's the thing, &lt;strong&gt;we humans love order.&lt;/strong&gt; In fact, we are so ordered that we maintain ourselves subconsciously. We follow the exact order to put our shoes on, we brush our teeth in the same rhythmic motion each day and even follow the same routine when we step out of a room.&lt;/p&gt;

&lt;p&gt;The moment, our schedule is interrupted, we divert from the plan as we no longer would get back the same one hour hence we tend to postpone it to the next available day. Here's where the issue lies-&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you miss a day that's twice as more harmful than getting your work done even for 5 minutes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Contingency Plan
&lt;/h2&gt;

&lt;p&gt;We know that emergencies and work can pop up anytime, regardless of how planned or how well managed our schedule or routine is. So, what can we do to dampen the damage?&lt;/p&gt;

&lt;p&gt;Luckily, we have a choice. I like to call it the &lt;strong&gt;Contingency Plan&lt;/strong&gt;. The contingency plan is a normal plan but only triggered when the normal routine is hindered. The idea to make this plan is to narrow down on the important, the necessary, and the extras. Here's an image to summarize how you create them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MgHJG1zo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/002/527/original/imag_3.png%3F1635369933" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MgHJG1zo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/002/527/original/imag_3.png%3F1635369933" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our goal is to be consistent not to be a perfectionist while trying to make a habit. Once we hit the threshold for it to be a habit, the skill we are trying to achieve would be subconsciously done. On that note, when we are working against time to manage the change in schedule, the idea is to follow the easiest path to be consistent i.e. to be present to make sure we are attending to the things we want to achieve.&lt;/p&gt;

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

&lt;p&gt;In a nutshell, to achieve something you have dreamt of achieving or you want to achieve, just be sure to be consistent.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember, consistency can move mountains.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's another picture from the book Atomic Habits by James Clear which tells you how much showing up consistently with 1% improvement can help you grow in a year compared to disjointed performances.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v9nc7mGJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/001/901/original/image2.png%3F1635366750" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v9nc7mGJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/012/001/901/original/image2.png%3F1635366750" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope that that this helps you understand and plan out the stuff you want to start. Take a week may be two, to understand how you'd like to start your journey and then stick to it. &lt;/p&gt;

&lt;p&gt;If you have any interesting suggestions or feedbacks, feel free to connect with me on &lt;a href="https://twitter.com/__xSpace"&gt;Twitter&lt;/a&gt;. I also have a newsletter, which I send out every week Thursday. You can subscribe it &lt;a href="https://www.getrevue.co/profile/xSpace/"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;See you next week! 👋🏼&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>beginners</category>
      <category>motivation</category>
    </item>
    <item>
      <title>The Weaponization of Functions</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Thu, 25 Nov 2021 09:19:30 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-weaponization-of-functions-3m4p</link>
      <guid>https://dev.to/snehangsude/the-weaponization-of-functions-3m4p</guid>
      <description>&lt;h2&gt;
  
  
  Section 3: Advanced Functions 🛠
&lt;/h2&gt;

&lt;p&gt;"The Art of Functions" covered the basics of functions but we need to learn quite a few important utilities as we start using various libraries or write our own code. Today, we will dive into a few major concepts as we learn more about *args, **kwargs, Higher-order functions, and recursion.&lt;/p&gt;

&lt;h3&gt;
  
  
  *args:
&lt;/h3&gt;

&lt;p&gt;These allow unlimited positional arguments to be passed inside a function with input or output functions. *args are tuples and can be looped through.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3UchC4FZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/430/206/original/_args.png%3F1632948075" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3UchC4FZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/430/206/original/_args.png%3F1632948075" width="880" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*args are usually used when you'd like your user to input many arguments and make them work in a similar kind of way. To put it in easier terms, do one action with the arguments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For example&lt;/em&gt;, in the above image we only sum up the arguments. You can similarly set colors for an image with various hex codes as arguments.&lt;/p&gt;

&lt;h3&gt;
  
  
  **kwargs:
&lt;/h3&gt;

&lt;p&gt;These allow unlimited keyword arguments to be passed inside a function with input or output functions. **kwargs are dictionaries and can be used as a key, value pair.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CtzI93Uy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/430/724/original/_kwargs.png%3F1632950065" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CtzI93Uy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/430/724/original/_kwargs.png%3F1632950065" width="880" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**kwargs are usually used when you'd like your user to input many arguments but to make them work in various different ways. To put it in easier terms, do multiple actions with the arguments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For example&lt;/em&gt;, in the above image we sum and multiply the arguments. You can similarly set various properties such as the width, the height of an image with keyword arguments. These are extremely popular in Data Science libraries such as Seaborn and Matplotlib.&lt;/p&gt;

&lt;h3&gt;
  
  
  Higher-Order Functions:
&lt;/h3&gt;

&lt;p&gt;Higher-order functions are functions that take in other functions as a parameter.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2wbmArUi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/438/407/original/Higher-Order_Functions.png%3F1632989094" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2wbmArUi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/438/407/original/Higher-Order_Functions.png%3F1632989094" width="880" height="833"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Higher-order functions are usually used when you'd like your user's choice to be separate and divide the actions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For example&lt;/em&gt;, in the above image we use the &lt;code&gt;cal&lt;/code&gt; function to take another function &lt;code&gt;add&lt;/code&gt; or &lt;code&gt;mul&lt;/code&gt; and pass in the argument given by the user to return a value. These are generally used if you are using a GUI application to assign respective functions to a button or a label.&lt;/p&gt;

&lt;h4&gt;
  
  
  Points to remember:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Higher-order function takes other functions as parameters&lt;/li&gt;
&lt;li&gt;Remember to not add parenthesis inside the argument&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Recursion:
&lt;/h3&gt;

&lt;p&gt;Recursion is the method of calling a function within a function, similar to a while loop.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5ariSq9x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/438/499/original/Recursion.png%3F1632989536" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5ariSq9x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/438/499/original/Recursion.png%3F1632989536" width="880" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recursion is usually used when you'd like to loop in a particular set of actions until another set of actions is done. Recursion is extremely handy as they can work with multiple libraries without necessarily using the while-loop. Since Recursion is an important topic we will discuss more, while understanding Data Structures especially Binary Trees.&lt;/p&gt;

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

&lt;p&gt;Functions are generally used to avoid repeatable code which not only makes our code look ugly and extremely hard to debug but also helps with readability since they help summarize a particular set of work inside a defined function. In general, functions with outputs are widely used and are more popular as they serve greater flexibility towards clean code.&lt;/p&gt;

&lt;p&gt;This is only a brief overview of the power functions hold to make our life easier. We will dive more into how we can use them and learn more on where we can use them in the upcoming series understanding the nitty-gritty stuff.&lt;/p&gt;

&lt;p&gt;If you have any interesting suggestions or feedback, feel free to connect with me on &lt;a href="https://twitter.com/__xSpace"&gt;Twitter&lt;/a&gt;. I also have a newsletter, which I send out every week Thursday. You can subscribe it &lt;a href="https://www.getrevue.co/profile/xSpace/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope you enjoy this, see you next week! 👋🏼&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Art of Functions</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Thu, 18 Nov 2021 21:32:50 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-art-of-functions-2njg</link>
      <guid>https://dev.to/snehangsude/the-art-of-functions-2njg</guid>
      <description>&lt;h2&gt;
  
  
  Section 2: Functions ⚙
&lt;/h2&gt;

&lt;p&gt;Wondering, what's up with this weird ancient art? Well, they help portray how civilization evolved much like how we learned to evolve our ways to talk to machines. Like us humans divided our society to do specific tasks say carpenters to work with wood, barbers to trim our hairs, similarly, we can ask machines to work specifically on something or take a specific action when some work is given. As an example imagine, when we wake up, we follow the below step -&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get your bed done &amp;gt; Brush your teeth &amp;gt; Wash your face &amp;gt; Dry your face&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For machines, these are described as functions. To understand what a function is imagine a set of instructions that needs to be done when something happens.&lt;/p&gt;

&lt;p&gt;Now you might think why do we need to have functions? It's simple, however much we like to abuse &lt;strong&gt;Ctrl+C&lt;/strong&gt; &amp;amp; &lt;strong&gt;Ctrl+V&lt;/strong&gt; it's not a good practice to have repeatable code. Instead, every time you have to do the same action, you can simply call the function to make it work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fundamentals of Functions
&lt;/h2&gt;

&lt;p&gt;In Python, there are two kinds of functions - &lt;strong&gt;Built-in functions&lt;/strong&gt; and &lt;strong&gt;User-defined functions&lt;/strong&gt;. We will talk about Built-in functions in a later post.&lt;/p&gt;

&lt;p&gt;Today, we will discuss the three kinds of &lt;strong&gt;User-defined functions&lt;/strong&gt;, let's start with the basic one. To write any function we need to have a few important things in mind -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function should always start with &lt;code&gt;def&lt;/code&gt; word&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;def&lt;/code&gt; will be followed by the name of the function, recommended in all lower case&lt;/li&gt;
&lt;li&gt;The name of the function is followed by a parenthesis and colon &lt;code&gt;():&lt;/code&gt;, denoting the end of defining a function&lt;/li&gt;
&lt;li&gt;Any lines inside the function should be indented to work under the function call.&lt;/li&gt;
&lt;li&gt;Optional requirement, a docstring can be added right below the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how it would look like. 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QVWXNa4E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/289/911/original/Function__2301.png%3F1632427737" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QVWXNa4E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/289/911/original/Function__2301.png%3F1632427737" alt="An example of Python Function" width="880" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the important takeaway from here is how a function when defined doesn't do anything, which means that it would be defined inside the variable &lt;code&gt;wake_up&lt;/code&gt; when you run the file, however, you would need to call it to make sure it does what you have asked it to do. You can call it by writing the unique name of the function you have defined, followed by the parenthesis.&lt;/p&gt;

&lt;p&gt;Here, I've asked it to print &lt;strong&gt;&lt;em&gt;I woke up at 8 AM&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we jump into understanding the other two types of functions and when to use them, we need to understand two important concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Parameters&lt;/strong&gt; - Parameters are something that your function can take and work with, inside the function. Think of it as a variable inside a function that can take inputs or can be optional too.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arguments&lt;/strong&gt; - Arguments are the values that you would assign to your parameter when calling the function. Think of it as assigning a value to the pre-defined parameters. Arguments are again defined into two groups:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Positional Arguments&lt;/strong&gt; - Arguments that are called into functions based on the positions of the paraments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keyword Arguments&lt;/strong&gt; - Arguments that are called into functions using the same keyword that is used to define the parameters.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As we have grasped the basic idea of Parameters and Arguments let's dive into the next type of function &lt;em&gt;drum-roll&lt;/em&gt; -&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions with inputs 👇
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HQKnkIQe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/290/173/original/Function__2302.png%3F1632429172" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HQKnkIQe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/290/173/original/Function__2302.png%3F1632429172" alt="An example of Python function with input" width="880" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can see that &lt;code&gt;wake_up&lt;/code&gt; has two parameters &lt;code&gt;time&lt;/code&gt; &amp;amp; &lt;code&gt;meridian&lt;/code&gt;. You can see that &lt;code&gt;time&lt;/code&gt; requires a value however &lt;code&gt;meridian&lt;/code&gt; has a pre-assigned value of a string &lt;code&gt;'AM'&lt;/code&gt;, which means that the meridian is optional for you to assign. Here's how you can call this function:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calling the function with only the required data -&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;em&gt;Positional argument&lt;/em&gt;&lt;/u&gt;: &lt;code&gt;wake_up(12)&lt;/code&gt; --&amp;gt; This will print &lt;strong&gt;&lt;em&gt;I woke up at 12 AM&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;&lt;em&gt;Keyword argument&lt;/em&gt;&lt;/u&gt;: &lt;code&gt;wake_up(time=12)&lt;/code&gt; --&amp;gt; This will also print &lt;strong&gt;&lt;em&gt;I woke up at 12 AM&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Here, the 12 gets assigned to the time variable whereas the meridian uses the 'AM' as the default value.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calling the function with both data -&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;em&gt;Positional argument&lt;/em&gt;&lt;/u&gt;: &lt;code&gt;wake_up(12, 'PM')&lt;/code&gt; --&amp;gt; This will print &lt;strong&gt;&lt;em&gt;I woke up at 12 PM&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;&lt;em&gt;Keyword argument&lt;/em&gt;&lt;/u&gt;: &lt;code&gt;wake_up(meridian='PM', time=12)&lt;/code&gt; --&amp;gt; This will print &lt;strong&gt;&lt;em&gt;I woke up at 12 PM&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Here, the 12 gets assigned to the time variable and the meridian gets assigned to the 'PM' value. This is one of the benefits of using keyword variables as it doesn't require the position of arguments to match the position of parameters.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/u&gt;: Changing the positional argument &lt;code&gt;wake_up('PM', 12)&lt;/code&gt; would print --&amp;gt; &lt;strong&gt;&lt;em&gt;I woke up at PM 12&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions with inputs and outputs 👇
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dKN0XTSu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/291/108/original/Function__2303.png%3F1632434550" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dKN0XTSu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/291/108/original/Function__2303.png%3F1632434550" alt="An example of Python function with output" width="880" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is probably the most used kind of function and is really handy to work with. Almost every rule stays the same from when we defined the Fundamentals of Functions apart from the addition of one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As this gives us an output a command &lt;code&gt;return&lt;/code&gt; is added at the end which allocates the output to a variable outside the function which can then be re-used.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/u&gt; Anything after the command &lt;code&gt;return&lt;/code&gt; would be ignored by the console as the function would immediately end and allocate the value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calling the function with positional argument -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;returned_value = add(2, 3)&lt;/code&gt; --&amp;gt; This doesn't print any value but instead allocated the variable &lt;code&gt;returned_value&lt;/code&gt; with &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Try this out and see, how the print statement is absolutely ignored by the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calling the function with keyword argument -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;returned_value = add(number1 = 6, number2 = 3)&lt;/code&gt; --&amp;gt; This too doesn't print any value but instead allocated the variable &lt;code&gt;returned_value&lt;/code&gt; with &lt;code&gt;9&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;We have now understood the types of functions that we can write to help us talk to machines efficiently, without having to repeat ourselves. However, sometimes, we speak in languages our machines have a hard time understanding, those are bugs or errors in our code.&lt;/p&gt;

&lt;p&gt;Here are a few common ones for you to catch:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Missing an argument, if your function has parameters would pop a &lt;code&gt;TypeError&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Writing a Keyword argument before a Positional argument would result in &lt;code&gt;SyntaxError&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Any line of code after the &lt;code&gt;return&lt;/code&gt; command on the same indentation level would be ignored.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's an image that summarizes how functions are triggered and how they work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3OMIzavZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/291/499/original/Params.PNG%3F1632437863" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3OMIzavZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/291/499/original/Params.PNG%3F1632437863" alt="An example of how functions work" width="432" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code starts execution from the arrow tail and follows its head&lt;/li&gt;
&lt;li&gt;When it hits the star&lt;/li&gt;
&lt;li&gt;It jumps to see the function under the name of &lt;code&gt;sum&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Executes everything inside the function taking the arguments to replace the parameters&lt;/li&gt;
&lt;li&gt;Hits the &lt;code&gt;return&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;Allocates the value of the local variable &lt;code&gt;num&lt;/code&gt; to the global variable &lt;code&gt;addition&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it for Functions! Next, we will talk about more advanced function usages. Hope this has helped you understand how functions work &amp;amp; what exactly happens under the hood making our lives easier. If you have any interesting suggestions or feedbacks, feel free to connect with me on &lt;a href="https://twitter.com/__xSpace"&gt;Twitter&lt;/a&gt;. I also have a newsletter, which I send out every week Thursday. You can subscribe it &lt;a href="https://www.getrevue.co/profile/xSpace/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this message finds you in good health!&lt;/p&gt;

</description>
      <category>python</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Start of Something Beautiful</title>
      <dc:creator>Snehangsu De</dc:creator>
      <pubDate>Mon, 11 Oct 2021 09:51:50 +0000</pubDate>
      <link>https://dev.to/snehangsude/the-start-of-something-beautiful-4516</link>
      <guid>https://dev.to/snehangsude/the-start-of-something-beautiful-4516</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"For millions of years mankind lived just like the animals.&lt;br&gt;
Then something happened which unleashed the power of our imagination,&lt;br&gt;
We learned to talk."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Quoting from the lines of Pink Floyd, it's simple to understand how communication has shaped our society, likely the single most important thing that has made civilizations stand. While we have created types of communication for us humans, since the age of machines we have learned to communicate with machines too, and surprisingly it spans back to the 40s.&lt;/p&gt;

&lt;p&gt;I wanted to start with the history as to where do programming languages come from, as they help us understand why we are working with them. Plus it narrates a nice story on how far we have come. As we begin, today we will be discussing Import Statements. However, here's a quick look at what the first high-level language looked like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VONBmgmU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/100/168/original/hello_world_Plankuuul.png%3F1631652133" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VONBmgmU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/100/168/original/hello_world_Plankuuul.png%3F1631652133" alt="'Hello World' in Plankalkül"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Traumatizing, right? I agree we have come a long way from the above image to print('Hello, world!').&lt;/p&gt;

&lt;p&gt;Let's begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 1: Imports in Python 🐍
&lt;/h2&gt;

&lt;p&gt;To help us understand what and how imports work, let's assume we have a module (basically a sheet of code) named dinosaur.py that has&lt;/p&gt;

&lt;p&gt;&lt;code&gt;type = 20&lt;/code&gt; [A variable]&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def roar(name): &lt;br&gt;
     ... return f'{name} roars'&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;We can import this module in two different ways :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Import statement 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import dinosaur&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Import statement 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from dinosaur import roar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here are few general pointers for import -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imports are statements and not functions&lt;/li&gt;
&lt;li&gt;Importing &lt;code&gt;dinosaur.py&lt;/code&gt; creates a variable name &lt;code&gt;dinosaur&lt;/code&gt; and also creates another variable &lt;code&gt;type&lt;/code&gt; which is inside the &lt;code&gt;dinosaur.py&lt;/code&gt; module&lt;/li&gt;
&lt;li&gt;Importing creates a module object &lt;em&gt;(object can be defined as a blueprint)&lt;/em&gt; and sets a variable (the name of the module) to point to that module object. &lt;strong&gt;&lt;em&gt;[Defined in the 3rd point]&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Running &lt;strong&gt;Import statement 1&lt;/strong&gt; executes the module line by line. So, when I call the statement :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It defines the global variable type as an integer.&lt;/li&gt;
&lt;li&gt;Defines the global variable roar as a function that takes one argument. &lt;/li&gt;
&lt;li&gt;Then import defines a variable dinosaur, through whose attributes (&lt;code&gt;dinosaur.type&lt;/code&gt; and &lt;code&gt;dinosaur.roar&lt;/code&gt;) we can get to our variable and function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Running &lt;strong&gt;Import statement 2&lt;/strong&gt; executes the module but only by the respective variable &lt;code&gt;roar&lt;/code&gt;. So, when I call the statement :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It defines only the variable &lt;code&gt;roar&lt;/code&gt; to be used inside the program.&lt;/li&gt;
&lt;li&gt;It would not define a variable under the module name, so we would not have a variable named dinosaur anymore.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is one other rare way to import which should be avoided at all costs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from dinosaur import *&lt;/code&gt; [This causes to replace all variables in your local namespace]&lt;/p&gt;

&lt;p&gt;A namespace is the current *.py file we would be working on, or importing the statements to.&lt;/p&gt;

&lt;p&gt;Here's a picture to understand better how variables work inside namespaces when imported.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JVnLMzhO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/101/230/original/Figma-namespace.PNG%3F1631657793" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JVnLMzhO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/revue/items/images/011/101/230/original/Figma-namespace.PNG%3F1631657793" alt="Imports w.r.t Namespace"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope this has helped you understand how imports work. As this being my first article, if you have any interesting suggestions, feel free to connect with me on &lt;a href="https://twitter.com/__xSpace"&gt;Twitter&lt;/a&gt;. I also have a newsletter, which I send out every week Thursday. You can subscribe it &lt;a href="https://www.getrevue.co/profile/xSpace"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this message finds you in good health, see you next week!&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>imports</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
