DEV Community

Tushar Singh
Tushar Singh

Posted on

Python Technical Paper (OOPs)

Paper by Tushar Singh (susst94@gmail.com)

Introduction:

Today's programming languages are in some way shape or form, all derivatives of either C or C++ and use those as the base skeleton to build upon and design most languages. One such design feature which is especially prevalent in C++ and is simply too good to pass up and not integrate with the derived programming language is the concept of Object Oriented Programming. Objective Oriented Programming or simply called OOPs is a programming framework that allows for introducing structured and sequential programming with a more linear and distributed flow of control. This linear flow is achieved with the help of something called Classes and Objects. What set out as a neat little concept for introducing a linear and distributed flow of control, has become the backbone for most large-scale projects in today's day and age for improving efficiency, layout, and tracking capability to improve error detection and correction.

We shall now start diving a bit into the gift that C++ gave to the programming world and how it is integrated with one of the most widely used programming languages which is Python.

Classes and Objects:

To put it in as few words as possible, Classes are a relational blueprint of instance variables with method definitions that describe how these variables and methods interact with each other. We can also define the type of our instance variables if we want to for the sake of transparency and improving debugging time.

A very basic representation of a class looks something like this

class Test:

    def __init__(self, argument):

        self.value1 = argument


    def Testing(self, argument 1: DataType) -> Output_DataType:

        Function defintions

        return Data
Enter fullscreen mode Exit fullscreen mode

What we have defined over here is a basic class definition with the class name Test and defined an attribute associated with the class value1. Note: if you type this code block word for word it will not work, this is meant to provide a basic understanding for you to refer to in case you wish to test it out.

We use this definition to initialize and create objects in memory and those objects can store values based on the arguments provided. This same blueprint is adopted for all sorts of in-built data types in Python be it int, strings, lists, sets, tuples, dictionaries, etc. Everything reverts back to the point that everything in Python is an object including functions.

The syntax for creating an instance of an object is as follows:

""instance_name = Class_name(arguments)""

Test1 = Test("Random")
Enter fullscreen mode Exit fullscreen mode

In this scenario, we have created an instance Test1 of the class Test and provided "Random" as the input argument to be used for creating our object.

One thing to note, which is being repeated throughout this paper is that the objective for introducing Classes and Objects and integrating with any languages architecture is to help in introducing and adopting a clean structured format to not only improve the readability of our code but also help us in identifying the core areas of our code along with segmenting and identifying any crucial areas in terms of detecting errors and performing corrections. We shall now see the founding aspects upon which OOPs were built. These aspects will always be repeated over and over and made use of every step of the way.

  • Encapsulation:

When we say Encapsulation, we mean coupling and protecting. In short, our classes want to limit the level of access to the attributes of their objects. This enables a sense of security from other classes and their methods by preventing them from accessing any attributes.

  • Inheritance:

Perhaps the most important aspect of OOPs is the idea of allowing the adoption of the blueprint of any class with the help of inheritance. Since the aim of OOPs is to introduce a linear and distributed flow of control, it makes sense to allow classes to inherit (copy the blueprints) the definitions of other classes if they wish to do so.

  • Polymorphism:

The dictionary definition of Polymorphism is the state of existing in different forms. It so happens, that in essence that is what is being implemented in OOPs in terms of polymorphism. Consider the following definitions of two methods in a class


  def Testing(self, argument1, argument2):

        Body of function

        return value

  def Testing(self, argument1):

        Body of function

        return value
Enter fullscreen mode Exit fullscreen mode

The two functions while sharing the same behave differently depending upon runtime which function is called depending upon the number of arguments provided during the function call

  c = instance.Testing(a,b)
  d = instance.Testing(a)
Enter fullscreen mode Exit fullscreen mode

During the first instance call of object instance with the class Testing, it refers to the definition which has 2 arguments along with the object instance. Whereas the second function call, it refers to the method definition with only 1 argument list (excluding the object in the definition)

Conclusion:

This paper was meant to be an introductory paper for getting the reader just acquainted and perhaps curious enough for a deeper dive into the world of Object Oriented Programming. The primary focus was more on the need for having OOPs concepts and their underlying principle and need to be adopted by any developer in today's day and age.

Top comments (0)