<?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: shivambats</title>
    <description>The latest articles on DEV Community by shivambats (@shivambats).</description>
    <link>https://dev.to/shivambats</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%2F365363%2F1319c214-5db8-4caa-b933-cf5d9fa88a77.jpeg</url>
      <title>DEV Community: shivambats</title>
      <link>https://dev.to/shivambats</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivambats"/>
    <language>en</language>
    <item>
      <title>Python Descriptors: A practical guide to understand the core</title>
      <dc:creator>shivambats</dc:creator>
      <pubDate>Mon, 17 Aug 2020 11:03:11 +0000</pubDate>
      <link>https://dev.to/shivambats/python-descriptors-a-practical-guide-to-understand-the-core-4oc0</link>
      <guid>https://dev.to/shivambats/python-descriptors-a-practical-guide-to-understand-the-core-4oc0</guid>
      <description>&lt;h3&gt;
  
  
  Descriptors are somewhat of an advanced concept but easy to implement once understood, let’s understand the core of it with some real world example.
&lt;/h3&gt;

&lt;h1&gt;
  
  
  What are Python Descriptors?
&lt;/h1&gt;

&lt;p&gt;Descriptor in python is an object which implements any of the method in the descriptor protocol. Descriptors are a powerful and useful protocol and behind some of the most useful concepts of python such as classmethods, staticmethods, properties etc.. The descriptor protocol is said to be the implementation of any of the below methods:&lt;br&gt;
&lt;code&gt;__get__(self, instance, type)&lt;br&gt;
__set__(self, instance, value)&lt;br&gt;
__delete__(self, instance)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That means any of the object which contains the implementation of any of the given methods, that object is said to be a descriptor. The object for the class DescriptorExample below will be a descriptor.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The output of the following code will be,&lt;br&gt;
&lt;code&gt;setting the value to 5&lt;/code&gt;&lt;br&gt;
&lt;code&gt;{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As we instantiate the object of YourClass and in &lt;strong&gt;init&lt;/strong&gt; method we are trying to set the value of some_attr, and some_attr is the attribute which has been attached to the Descriptor and hence its behavior which is setting and retrieval will be overridden by the methods of the DescriptorExample.&lt;/p&gt;

&lt;p&gt;Note that the value of some_attr has not been set as of yet, since we have just put the print statement in its get and set methods. And that’s why your_class_obj.&lt;strong&gt;dict&lt;/strong&gt; gives us an empty dict.&lt;/p&gt;

&lt;p&gt;Let’s add the functionality to achieve the set and get behavior for the attribute of the instance.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The output of the following code will be,&lt;br&gt;
&lt;code&gt;setting the value to 5&lt;/code&gt;&lt;br&gt;
&lt;code&gt;{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As we instantiate the object of YourClass and in &lt;strong&gt;init&lt;/strong&gt; method we are trying to set the value of some_attr, and some_attr is the attribute which has been attached to the Descriptor and hence its behavior which is setting and retrieval will be overridden by the methods of the DescriptorExample.&lt;br&gt;
Note that the value of some_attr has not been set as of yet, since we have just put the print statement in its get and set methods. And that’s why your_class_obj.&lt;strong&gt;dict&lt;/strong&gt; gives us an empty dict.&lt;br&gt;
Let’s add the functionality to achieve the set and get behavior for the attribute of the instance.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The output of the following code will be,&lt;br&gt;
&lt;code&gt;setting the value to 5&lt;br&gt;
getting the value of the object's attribute&lt;br&gt;
5&lt;br&gt;
setting the value to 10&lt;br&gt;
getting the value of the object's attribute&lt;br&gt;
10&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we have extended the behavior of getting and setting the some_attr attribute.&lt;/p&gt;
&lt;h2&gt;
  
  
  Types of Descriptors
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Data Descriptor: If an object defines &lt;strong&gt;set&lt;/strong&gt;() or &lt;strong&gt;delete&lt;/strong&gt;(), it is considered as a data descriptor. All the descriptors defined earlier are data descriptor as we have implemented &lt;strong&gt;set&lt;/strong&gt; method in them.&lt;/li&gt;
&lt;li&gt;Non Data Descriptor: If an object defines only &lt;strong&gt;get&lt;/strong&gt;(), it is considered as a non data descriptor. We can also restrict the value assignment to the attribute.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Usage of Descriptor
&lt;/h1&gt;

&lt;p&gt;Let’s say we have orders data with its order_id, shipping_price, and order_price. And we need to store the pretax values of shipping_price and order_price. Also, the tax calculated for Europe region and US region are different. In this example, we will be assuming the tax rates for EU and US region to be 19 and 12 respectively.&lt;br&gt;
The usual way of doing this could be having a method to calculate pretax values of EU and US region. Something like this,&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The output of the snippet is given below, as you can see we do get the pretax value after the first assignment of the attribute, but once the value of shipping_price is assigned again, we do not get the pretax value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;25.0&lt;br&gt;
20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However the problem with this approach is if we assign a value to shipping_price or order_price later to the instance, it will not have a pretax value and instead the value which is assigned to it. Hence we will have sort of a broken class.&lt;br&gt;
We can try to solve this problem by using descriptors, and having descriptors for both EU and US attributes. Something like this.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The output of the following will be, note that setting the value of shipping_price to be 11 and 22, we get the pretax values for both the attributes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;20.0&lt;br&gt;
25.0&lt;br&gt;
10.0&lt;br&gt;
20.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you can see that we have reused the code for the marketplaces in the US, this way any new marketplace added for any of the US or EU region can simply attach their attribute whose pretax value need to be stored. This is nothing less than the magic of descriptors.&lt;br&gt;
Descriptors can have a lot of other uses such as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data Validation — Validating the data before setting the value of the attribute.&lt;/li&gt;
&lt;li&gt;Restrict Access — As shown above, we can restrict the assignment of any attribute by using non data descriptor.&lt;/li&gt;
&lt;li&gt;Lazy Loading — We can put the business logic in the get method rather than set so we only compute while getting the data.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Using Python’s Magic Methods to compare Tesla and Ford</title>
      <dc:creator>shivambats</dc:creator>
      <pubDate>Sat, 15 Aug 2020 19:58:34 +0000</pubDate>
      <link>https://dev.to/shivambats/using-python-s-magic-methods-to-compare-tesla-and-ford-338f</link>
      <guid>https://dev.to/shivambats/using-python-s-magic-methods-to-compare-tesla-and-ford-338f</guid>
      <description>&lt;p&gt;Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘&amp;lt;’, ‘&amp;gt;’, ‘==’, ‘+’ etc..&lt;/p&gt;

&lt;p&gt;You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like &lt;code&gt;__init__&lt;/code&gt;, &lt;code&gt;__call__&lt;/code&gt;, &lt;code&gt;__str__&lt;/code&gt; etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).&lt;/p&gt;

&lt;p&gt;Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;__init__&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AnyClass:
    def __init__():
        print("Init called on its own")
obj = AnyClass()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The first example is &lt;strong&gt;init&lt;/strong&gt;, and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the &lt;strong&gt;init&lt;/strong&gt; method is called on its own.&lt;/p&gt;

&lt;p&gt;The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.&lt;br&gt;
Init called on its own&lt;/p&gt;
&lt;h2&gt;
  
  
  2. &lt;code&gt;__add__&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s move to some other example, &lt;strong&gt;add&lt;/strong&gt; gives us the ability to access the built in syntax feature of the character +. Let’s see how,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AnyClass:
    def __init__(self, var):
        self.some_var = var
    def __add__(self, other_obj):
        print("Calling the add method")
        return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;As we use the + character, it will call the &lt;strong&gt;add&lt;/strong&gt; method. Now observe the &lt;strong&gt;add&lt;/strong&gt;, it expects the second argument to be other_obj, and hence we have the access to add not just some_var of the other object but any other variable as well. The output would be,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Calling the add method"
11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Similar to &lt;strong&gt;add&lt;/strong&gt;, we can have&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;__eq__&lt;/code&gt;(self, other_obj)Defines behavior for the equality operator, ==.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__ne__&lt;/code&gt;(self, other_obj)Defines behavior for the inequality operator, !=.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__lt__&lt;/code&gt;(self, other_obj)Defines behavior for the less-than operator, &amp;lt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__gt__&lt;/code&gt;(self, other_obj)Defines behavior for the greater-than operator, &amp;gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__le__&lt;/code&gt;(self, other_obj)Defines behavior for the less-than-or-equal-to operator, &amp;lt;=.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__ge__&lt;/code&gt;(self, other_obj)Defines behavior for the greater-than-or-equal-to operator, &amp;gt;=.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  3. &lt;code&gt;__repr__&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;It is used to represent the object. It gives all the representation of all the information of the object. We will understand more about it in the example later.&lt;br&gt;
If we create an object of the class AnyClass from the previous example, and do repr(obj) or print(obj) it will return&lt;br&gt;
&lt;code&gt;'&amp;lt;__main__.AnyClass object at 0x0000025B8FB75048&amp;gt;'&lt;/code&gt;&lt;br&gt;
Let’s add the &lt;strong&gt;repr&lt;/strong&gt; method to our AnyClass, and see the ‘magic’.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AnyClass:
    def __init__(self, var):
        self.some_var = var
def __repr__(self):
        print(f"AnyClass with some_var value as {self.some_var}")
obj = AnyClass(5)
print(obj)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The output of the above example will be,&lt;br&gt;
&lt;code&gt;AnyClass with some_var value as 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s use these magic methods and compare Tesla Model 3 and Ford Mustang&lt;/p&gt;

&lt;p&gt;Lets create a class Car and define these magic methods to get the ability to use the operators and compare the Car’s attributes such as Top Speed, Horsepower and Price.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car:
    def __init__(self, name, manufacturer, horsepower, top_speed, cost, length):
        self.name = name
        self.manufacturer = manufacturer
        self.horsepower = horsepower
        self.top_speed = top_speed
        self.cost = cost
        self.length = length
    def __sub__(self, other):
        print(f"Difference In Price: {self.cost - other.cost}")
        print(f"Difference in Horsepower: {self.horsepower -     other.horsepower}")
        print(f"Difference in Top Speed: {self.top_speed -   other.top_speed}")
    def __len__(self):
        return f"{self.length}m"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we run this program and command,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tesla = Car(name='Model 3', manufacturer='Tesla', horsepower=283, top_speed=129, cost=35000, length=4.69)
ford = Car(name='Mustang Mach E', manufacturer='Ford', horsepower=282, top_speed=132, cost=25100, length=4.72)
ford - tesla
len(ford)
len(tesla)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Will return us,&lt;br&gt;
&lt;code&gt;Difference In Price: $8895&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Difference in Horsepower: -1&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Difference in Top Speed: 3&lt;/code&gt;&lt;br&gt;
&lt;code&gt;4.72m&lt;/code&gt;&lt;br&gt;
&lt;code&gt;4.69m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The ‘-’ operator gave us the difference in price, power and speed, it’s nothing less of a ‘magic’ if you don’t know how it works. But now we do know, and lets implement other methods. And len gave us the lengths of the cars ie. the length attribute of the Car object.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;By this program, we will be able to run commands such as,&lt;code&gt;tesla == ford&lt;br&gt;
tesla != ford, tesla &amp;lt; ford&lt;/code&gt; etc..&lt;/p&gt;

&lt;p&gt;Note this might not be something that you will have to do in real life but this is one of the examples to showcase how we can make use of the built in features of Python, and understand the concepts behind them.&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Handling exceptions in Python a cleaner way, using Decorators</title>
      <dc:creator>shivambats</dc:creator>
      <pubDate>Fri, 14 Aug 2020 20:21:06 +0000</pubDate>
      <link>https://dev.to/shivambats/handling-exceptions-in-python-a-cleaner-way-using-decorators-2l09</link>
      <guid>https://dev.to/shivambats/handling-exceptions-in-python-a-cleaner-way-using-decorators-2l09</guid>
      <description>&lt;h2&gt;
  
  
  Functions in Python
&lt;/h2&gt;

&lt;p&gt;Functions in Python are first class objects, which means they can be assigned to a variable, passed as an argument, and return from another function and store that in any data structure.&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_function&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;"Example Function called"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;some_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;example_function&lt;/span&gt;
&lt;span class="n"&gt;some_variable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see we have assigned example_function to some_variable which makes some_variable callable. The output for the following will be:&lt;br&gt;
&lt;code&gt;Example Function called&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Decorators
&lt;/h2&gt;

&lt;p&gt;The first class object property of function helps us to use the concept of Decorators in Python. Decorators are functions which take as argument another function as an object, which enables us to put our logic either at the start and end of the execution of the argument 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;decorator_example&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;"Decorator called"&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_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&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;"Calling the function"&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&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;"Function's execution is over"&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_function&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;decorator_example&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&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;"Executing the function"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Function logic goes here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see we are using the decorator by using @decorator_example on top of the function which needs to be passed as the argument to the Decorator function. In this case, some_function will be passed as an argument to decorator_example. The output of the above snippet will be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Decorator called&lt;br&gt;
Calling the function&lt;br&gt;
Executing the function&lt;br&gt;
Function's execution is over&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Error Handling Using Decorators
&lt;/h2&gt;

&lt;p&gt;You can use Decorators for quite a lot of purposes like logging, validations, or any other common logic which needs to be put in multiple functions. One of the many areas where Decorators can be used is the exception handling.&lt;br&gt;
Let’s take an example of such functions, which require handling of the same exceptions.&lt;/p&gt;

&lt;p&gt;We will take a simple example of calculating areas. And we will be for now printing the errors if an unsupported type is passed as the argument which in this case will be a string.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Errors should never pass silently.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Unless explicitly silenced.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The normal way to do this would be to have all such functions in a try-catch, somewhat like this,&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;area_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&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;length&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;TypeError&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;"area_square only takes numbers as the argument"&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;area_circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&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="mf"&gt;3.142&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;TypeError&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;"area_circle only takes numbers as the argument"&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;area_rectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&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;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;TypeError&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;"area_rectangle only takes numbers as the argument"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, this looks repetitive which we should avoid to the extent we can. So we can use the magic of Decorators here and observe that the code looks a lot cleaner this way. And a clean code goes a long way.&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;exception_handler&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_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;try&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;TypeError&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"&lt;/span&gt;&lt;span class="si"&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;__name__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; only takes numbers as the argument"&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_function&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;exception_handler&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;area_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&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;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;exception_handler&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;area_circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&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="mf"&gt;3.14&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;exception_handler&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;area_rectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;breadth&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;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;area_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;area_circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;area_rectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;area_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some_str"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;area_circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some_other_str"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;area_rectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some_other_rectangle"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The output of the following will be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;4&lt;br&gt;
12.568&lt;br&gt;
8&lt;br&gt;
area_square only takes numbers as the argument&lt;br&gt;
area_circle only takes numbers as the argument&lt;br&gt;
area_rectangle only takes numbers as the argument&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can extend the capability of raising errors in the exception_handler by having custom exceptions and further expand its usages. This was one of the examples which enables us to handle the exceptions cleanly.&lt;/p&gt;

</description>
      <category>python</category>
      <category>design</category>
      <category>webdev</category>
      <category>django</category>
    </item>
  </channel>
</rss>
