Part-II- Python: Object Oriented Programming


In part-I of this series, we built a basic class in Python and compared the code with a similar class in C#. In this article, we derive the basic class built in part-I to create a new CAdvancedMath to which we add a function to find the factorial of an integer number.

The base class code for Python and C# that we used in part-I is given below:

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);
        }
    }  
}

We create a new class CAdvancedMath that derives from CBaseMath. Note that we use the base class 'as-is' without changing any code. Code for the C


Python
class CAdvancedMath(CBaseMath):
    def __init__(self,sigDigits):
        CBaseMath.__init__(self,sigDigits)

    #Note: This function cannot access private members in base class
    #      and Python does not support a protected type
    def factorialInt(self,value):
        finalValue=1
        #The -1 parameter is required to decrease the value
        for val in range(value,1,-1):
            finalValue = self.multiply(finalValue,val)
        return finalValue
       
C#
public class CAdvancedMath : CBaseMath
    {       
        public CAdvancedMath(int sigDigits) : base(sigDigits)
        { }

        //Note: This function can access private members in base class
        //      if they are specified as protected. 
        public double FactorialInt(int value)
        {
            double finalValue = 1;
            
            for(int val=value;val>1;val--)
            {
                finalValue = Multiply(finalValue, val);
            }
            return finalValue;
        }
    }
       

The Python code clearly shows the different syntax for deriving from a base class. also note the way the base class constructor is called using CBaseMath.__init__(self,sigDigits).

The full code to instantiate the classes and call the functions of the derived CAdvancedMath class is:

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

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

The outputs for the respective codes is given as:

Python
Sum is :3
Sum is :3.0
Sum is :3.0
Factorial is :24
       
C#
Sum is :3
Sum is :3
Sum is :3
Factorial is :24       

The printout of the 2nd and 3rd values are 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
am = CAdvancedMath(3)
print("Sum is :" + str(am.sum(1,2)))
print("Sum is :" + str(am.sum(1.01,2.01)))
print("Sum is :" + str(am.sum(1.001,2.001)))
print("Factorial is :" + str(am.factorialInt(4)))
       
C#
using System;

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

And the corresponding output is:

The outputs for the respective code bases is given as:

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

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



As we can see from the screen captures, the code for both classes is contained in the same file. In part-III, we see how we can link code between different Python files.

Comments

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