Object-Oriented Programming
Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects
and classes in programming.
OOPs Concepts in Python
1. Class: A class is a collection of objects. A class contains the blueprints or the prototype
from which the objects are being created.
2. Objects: Objects are the instances of a particular class. It has a state and behavior
associated with it. State is represented by the attributes of an object. Behavior is
represented by the methods of an object.
3. Polymorphism: Polymorphism refers to the use of a single type entity like method or
operator to represent different forms
4. Encapsulation: Encapsulation is bundling of data members and functions inside a
single class
5. Inheritance: Inheritance is the capability of one class to derive or inherit the properties
from another class. The class that derives properties is called the derived class or child
class and the class from which the properties are being derived is called the base class or
parent class.
6. Data Abstraction: Abstraction in python is defined as hiding the implementation of logic
from the client
Creating Class:
Class with attributes
class Mobile:
pass
mob1=Mobile()
mob2=Mobile()
mob1.price=20000
mob1.brand="Apple"
mob2.price=3000
mob2.brand="Samsung"
print(mob1.price)
print(mob1.brand)
print(id(mob1))
class Mobile:
def __init__(self, brand, price):
self.brand = brand
self.price = price
mob1=Mobile(‘Apple’, 2000)
mob2=Mobile(‘Samsung’,3000)
print(mob1.brand)
print(mob1.price)
__init__()
When we create an object, the special __init__() method inside the class of that object is invoked
automatically. This special function is called as a constructor.
self
self represents the instance of the class. By using the “self” one can access the attributes and
methods current working class object
Class with methods
class Mobile:
def __init__(self, brand, price):
self.brand = brand
self.price = price
def mob_info (self):
print(self.brand)
print(self.price)
mob1=Mobile(‘samsung’,20000)
mob1. mob_info()