Part-I-Python: Object Oriented Programming


Due to its simplicity and versatility, Python has become a very popular language among professional software developers and non-developers alike. Python supports the imperative semantics of programming though it has reasonable support for functional programming.

In the imperative semantics of programming, Python allows for both procedural (C-style) as well as object-oriented (C++/C#/Java style) programming. While the procedural style of programming in Python is simple and does not use specific semantics, the object-oriented (OO) style does have a small learning curve which in understanding Python specific syntax. Note that just like in C and other procedural languages, the procedural style is a subset of the OO style, implying that all rules/syntax for procedural style apply in OO style.

In this article, we look at object-oriented programming in Python. We use a simple example and also compare the similar code in C#. The reason for choosing C# is that the language is a cross between C++ and Java, and most of the syntax of C# can be applied to C++ and Java.

The code we are listing is for building a class to do basic math operations of addition, subtraction, multiplication and division. We use the class constructor to initialize the significant digit that each of the 4 computation routines will return the results in. The significant digit conversion is done in a separate private function.

The following table lists the code for Python and its corresponding C# Code. Note that the function and variable names reflect the generally accepted coding standards for these two development platforms.

Python
class CBaseMath:
    #Constructor
    class CBaseMath:
    def __init__(self, sigDigits):
        self.__sigDigits=sigDigits

    #This is a private function
    #There is no keyword in Python to make this protected to
    #be accessible by derived class
    def __setRounding(self,value):
        return round(value,self.__sigDigits)

    def sum(self,a,b):
        return self.__setRounding(a+b)

    def substract(self,a,b):
        return self.__setRounding(a-b)

    def multiply(self,a,b):
        return self.__setRounding(a*b)

    def division(self, a,b):
        return self._setRounding(a/b)
       
C#
using System;

namespace OOPSample
{
    public class CBaseMath
    {
        private int _sigDigits;

        //Constructor
        public CBaseMath(int sigDigits)
        {
            _sigDigits = sigDigits;
        }

        //This is a private function
        //Note: C# double is 64-bit
        private double SetRounding(double value)
        {
            return Math.Round(value, _sigDigits);
        }

        public double Sum(double a, double b)
        {
            return SetRounding(a + b);
        }

        public double Substract(double a, double b)
        {
            return SetRounding(a - b);
        }

        public double Multiply(double a, double b)
        {
            return SetRounding(a * b);
        }

        public double Division(double a, double b)
        {
            return SetRounding(a / b);
        }
    }  
}
       

Note the difference in the constructor declaration as well as the private function. Functions starting and trailing with underscores like __init__ are special and can be considered as 'reserved' in Python. Any function or variable starting with double underscores('__') is equivalent to private function to variable. This way the function __setRounding(self,value) and variable __sigDigits are private and cannot be accessed from outside through the class instance. The keyword self is equivalent to (but not same) as the keyword this in object-oriented languages.

Note how this complies with the elegant pattern in Python where code formatting is used to enforce its purpose. This elegance and simplicity allows the enforcement of a coding standard as well as definition of type.

The full code to instantiate the classes and call the functions is:

Python
bm = CBaseMath(2)
print("Sum is :" + str(bm.sum(1,2)))
print("Sum is :" + str(bm.sum(1.01,2.01)))
print("Sum is :" + str(bm.sum(1.001,2.001)))
       
C#
using System;

namespace OOPSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CBaseMath bm = new CBaseMath(2);
            Console.WriteLine("Sum is:" + bm.Sum(1, 2));
            Console.WriteLine("Sum is:" + bm.Sum(1.01, 2.01));
            Console.WriteLine("Sum is:" + bm.Sum(1.001, 2.001));
            Console.ReadLine();
        }
    }
}

The outputs for the respective codes is given as:

Python
Sum is :3
Sum is :3.02
Sum is :3.0
       
C#
Sum is :3
Sum is :3.02
Sum is :3
       

The printout of the 3rd value is different between Python because of the way the round function works in both platforms.

If we change the significant digits to 3, the full code to instantiate the classes and call the functions is:

Python
bm = CBaseMath(3)
print("Sum is :" + str(bm.sum(1,2)))
print("Sum is :" + str(bm.sum(1.01,2.01)))
print("Sum is :" + str(bm.sum(1.001,2.001)))
       
C#
using System;

namespace OOPSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CBaseMath bm = new CBaseMath(3);
            Console.WriteLine("Sum is:" + bm.Sum(1, 2));
            Console.WriteLine("Sum is:" + bm.Sum(1.01, 2.01));
            Console.WriteLine("Sum is:" + bm.Sum(1.001, 2.001));
            Console.ReadLine();
        }
    }
}

And the corresponding output is:

The outputs for the respective codes is given as:

Python
Sum is :3
Sum is :3.02
Sum is :3.002
       
C#
Sum is :3
Sum is :3.02
Sum is :3.002
       

The following screen shows the PyCharm and Visual Studio IDE's with the solution containing this code.



In part-II, we will look at how class inheritance is implemented in Python.


Comments

  1. Nice article I was impressed by seeing this blog, it was very interesting and it is s for sharing all the information with us all.very useful for me. Thank you for sharing any good knowledge and thanks for fantastic efforts.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore


    ReplyDelete
  2. Hi, I read your whole blog. This is very nice. Good to know about the career in qa automation is broad in future. We are also providing various Python Training, anyone interested can Python Training for making their career in this field .

    ReplyDelete

Post a Comment

Popular posts from this blog

Part III: Backpropagation mechanics for a Convolutional Neural Network

Introducing Convolution Neural Networks with a simple architecture

Deriving Pythagoras' theorem using Machine Learning