Objected Oriented Programming (OOP) in Python
Python supports object-oriented programming (OOP).
Class
A class is a real or a virtual entity, which has relevance to the problem at hand. For example, when you develop software for a car company, then 'Car', is central to the software and therefore there will be a class called 'Car'. Another example is that when developing a student management system, a 'Student' class is created.
A class has many components, most important of which are attributes and functions.
Attributes
The attributes are the characteristics of the entity. For example, when creating a class that gives the details of movies, a
class 'Movie' will be needed with attributes like name, year, genre, director, producer, actors, music director, and story writer.
Functions
The functions accomplish a particular task. In a class there can be any number of functions, each accomplishing a particular task. There are special functions for initializing the data members of a class as well.
Object
An object is an instance of a class. The objects interact with each other and get the work done. Generally, a class can have any number of objects. In object-oriented paradigm, the program revolves around an object and therefore the type of programming is termed as an object-oriented program.
Encapsulation
The class is an entity, which has both data and functions. The clubbing together of the data and the functions that operate on the data is called encapsulation.
Inheritance
Classes are made so that they can be sub-classed. This art of dividing the class into subclasses is inheritance. For example, the movie class (base class) can be sub-classed into various classes like art_movie or commercial_movie.
Polymorphism
Poly means many and morphism is forms, so polymorphism means many forms. Polymorphism can be implemented in many ways. For example, operator overloading, which means using the same operator in more than one way. For example “+” is used between integers to add, with strings for concatenation.
Function overloading means having more than one function with the same name in a class with different arguments.
Python Class
It is easier to make a class in Python than in any other programming languages. A class in Python can hold any kind and any amount of data. A class in Python can be sub-classed. All types of inheritance, including multiple inheritances, are supported in Python. Method overriding is also allowed in Python.
In a class all data members are public in nature, so they can be accessed anywhere in the program. The member functions in a class are all virtual. In a class all the member functions must have the first argument as the object representing that class, referred to as "self".
How to Define a Class?
In Python, a class can be defined using the class keyword. The class keyword is followed by the name of the class. The body of the class follows. It must have proper indentations.
Creating an Object
An object is created by associating a name with an instance of the class, initialised using the default constructor.
Constructor
In Python, the constructors are implemented by making the _init_() function inside the class.