Part-III - Python: Object Oriented Programming


In part-II of this series, we saw how inheritance works when we derive from a single base class. In part-II, the base and derived classes were all declared in the same file. In this article, we look at how classes can be declared in their own files and how they reference each other. The ability to reference source code in multiple files is a very important requirement towards building complex scalable applications.

Our project solution will the have following 3 files:

Python C# Comments
CBaseMath.py CBaseMath.cs Base class code
CAdvancedMath.py CAdvancedMath.cs Derived class code
Program.py Program.cs Code to instantiate and call functions

The code for each of these files is given below:

CBaseMath.py / CBaseMath.cs
Python
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;

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

Since CBaseMath is a base class it does not need to reference other classes. The definition for next class will reference CBaseMath and note the difference in classes residing in different source code files are referenced.

CAdvancedMath.py / CAdvancedMath.cs

Python
from CBaseMath import CBaseMath

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#
namespace OOPSample
{
    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;
        }
    }
}
       

Note the important line from CBaseMath import CBaseMath in the Python code. This tells the Pythong interpreter to load the class CBaseMath from the file 'CBaseMath.py'. In C#, since both CBaseMath and CAdvancedMath are in the same namespace we do not need to add an explicit reference to load CBaseMath. The same syntax will be used in 'Program.py'.

Program.py / Program.cs
Python
from CAdvancedMath import CAdvancedMath

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("Factorial is:" + am.FactorialInt(4));
            Console.ReadLine();
        }
    }
}

Since we are instantiating only the CAdvancedMath class, we need access only to the 'CAdvancedMath.py' file. This is achieved by the from CAdvancedMath import CAdvancedMath declaration. As before due to same namespaces, C# does not need an explicit declaration to include CAdvancedMath.

The outputs for the respective code bases 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 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 multiple files are listed in the project tree.

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