Posts

Showing posts with the label C#

Part-III - Python: Object Oriented Programming

Image
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 protec...

Part-II- Python: Object Oriented Programming

Image
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# usi...

Part-I-Python: Object Oriented Programming

Image
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 betwe...