Object Oriented Programming in Python

As in Python it is not necessary to create our code into classes while developing program as we can use functions also which is known as Procedural Programming. However, a procedural programming is used for writing small, short and simple programs whereas object oriented programming (OOP) programs becomes more important as the programs grows in size and complexity. The Python has been the object oriented language since it is developed.

So let’s have small introduction about object oriented programming concepts used-

Classes

In comparison to other programming languages, the concept of classes in Python is added with minimum of new syntax and semantics. The concept of classes in Python is a mixture of classes in C++ and Modula-3. The Python classes provide all the basic features of OOP such as class inheritance which allows multiple base classes, a derived class which can override any method of its base class and the method which can call the method of a base class with the same name.

First look at classes

In Python classes are introduced with new syntax and semantics.

Class definition syntax:

Example:

Given below is the example of a simple Python class:-

In the above code the variable stuCount is a class variable whose value is shared among all instances of a student class. The variable can be accessed as Student.stuCount from inside the class or outside of the class. The first method init( ) is a special method which is called class constructor or the initialization method which is called when you create a new instance of the class. The self argument is added by the Python itself when the methods are called.

Objects

Objects are the basic building block of Python object oriented program.

Attributes

Attributes is the feature of an object. The _init_( ) method is used to initialize the attributes of an object. To access the object’s attributes we use the dot operator with the object. Like

So the complete program is:

Inheritance

Inheritance another feature of an OOP in Python where it a way to build new classes from the existing classes and they are named as derived classes. The derived classes are derived or inherited from base classes. The main advantage of inheritance is that the code can be reused and the complexity of the program can be reduced. The derived classes extends the functionality of base class.

Example:

Output:

Polymorphism

The polymorphism is a process where a function is used in different ways for different inputs. Basically, polymorphism is if a class B is inherited from class A so it does not inheriting everything of class A it can inherit some of the functions of class A.

Example:

Output:

Operator Overloading

In Python classes can have the operations with the special method names but these methods cannot be called directly but by a specific syntax.

Example:

 

1 Comment Object Oriented Programming in Python

Leave A Comment

Your email address will not be published. Required fields are marked *