29 Sep 2020

OOP using Python - Part -1

Object-Oriented Programming

Object Oriented Programming or OOP, is a programming paradigm that includes or relies, on the concept of classes and objects.

Python supports different programming approaches and is sometimes referred to as a multi-paradigm programming language. The basic idea behind OOP is to divide a rather sophisticated program into a bunch of objects communicating with each other in order to implement real-world scenarios using code.

An object has two major characteristics:

  • attributes
  • behavior

For example: A pet dog can be an object of Dog class with it’s breed, age, color, name etc. as it’s attributes and barking, digging, panting etc. as it’s behavior.

Some examples of Object Oriented Programming languages are:

  • C++
  • Java
  • Ruby
  • C#
  • Python

Let’s dig into the concepts of OOP using Python.


Classes and Objects

Everywhere I go, I see an object. Everything in our surrounding is an object. These objects have certain properties that define them and they perform specific tasks or certain actions can be performed on them as well.

A class is a blueprint of an object, it contains all the properties about the object. An object is an instantiation of a class. When a class is defined, we only write the description of the object inside it, therefore, no memory or storage is allocated to a class. Memory is allocated to the object itself.

Example:


Output:
Akamaru is a Ninja Hound of Great Pyrenees breed. He is 7 years old and White in color

 Dogs are of Canis familiaris specie

Properties

Properties are variables that contain information regarding the object of class. Like in the above example, specie is a property of the class.

Methods

Methods or functions have access to those properties or other methods of the class. Methods can define the behavior of an object.

Advantages of using Classes and Objects

The concepts of Object Oriented Programming allows us to write complex applications using Python. Classes and objects are the basic building block behind all the principles of Object oriented programming.

  • Compartmentalize the code.
  • Code reusability
  • Easily Maintainable

Disadvantages

  • Lots of boilerplate code.
  • Multiple layers of abstraction hinders with readability.

That’s all for now. In the next blog, we will be writing on class methods, static methods and access modifiers.

Keep learning. Have a nice day. :)


Tags: