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.
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:
The outputs for the respective codes is given as:
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:
And the corresponding output is:
The outputs for the respective codes is given as:
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.
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.
ReplyDeleteoracle 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
Thanks again for the article post.Really thank you! Fantastic.
ReplyDeletejava training
mulesoft training
etl testing training
This is great stuff. thanks for sharing this content
ReplyDeletePython Online Training Hyderabad
Best Python Online Training
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 .
ReplyDeleteNice post.Thanks for the post.
ReplyDeletePython Course in Nagpur