Python - OOPs

Python is an Object-Oriented Programming Language, It breaks large programs into classes and modules. Object-Oriented Programming provides features like classes, objects, inheritance, polymorphism, which is useful for handling large programs.

Python - Classes & Objects

Class is a user-defined data type in which we group different methods and variable. Objects are the run time entities which is used to access the class members.

Example 1: Create a Python Class, inside class create a method that display a welcome message.

class Welcome:
    def message(self):
        print("This is a Welcome Message inside a Class")

obj=Welcome() #class object declared
Welcome.message(obj) #calling class method
obj.message() #calling class method
This is a Welcome Message inside a Class
This is a Welcome Message inside a Class

Example 2: Find sum of 2 numbers using class.

class Sum:
    def add(self, a, b):
        c=a+b
        print("Sum =",c)

a=int(input("Enter 1st number: "))
b=int(input("Enter 2nd number: "))
obj=Sum()  #object created
obj.add(a, b)
Enter 1st number: 10
Enter 2nd number: 20
Sum = 30

Example 3: Create a Python Constructor

class Computer:
    def __init__(self, name, age):
        self.name=name
        self.age=age
    
    def display(self):
        print(self.name)
        print(self.age)

com1=Computer('user1', 20)
com1.display()

com2=Computer('user2', 30)
com2.display()

Example 4: Passing object in method and compare two objects age.

class Computer:
    def __init__(self, name, age):
        self.name=name
        self.age=age
    
    def compare(self, obj2):
        if self.age==obj2.age:
            return True
        else:
            return False

com1=Computer('user1', 20)
com2=Computer('user2', 30)
if com1.compare(com2):
    print("Equal")
else:
    print("Not Equal")

Class or Static variable

S.No Instance Variable Static Variable
Instance variable is defined inside __init__ method Static variable is defined outside __init__ method, but inside a class
In instance variable we can assign different values for different objects Value of Static variable is common for all objects

Example 5: Class or static variable.

class Student:
    subject='python'  #class variable or static variable
    def __init__(self, name, age):
        self.name=name #instance variable
        self.age=age

s1=Student('user1', 20)
s2=Student('user2', 30)
print(s1.name, s1.age, s1.subject)
print(s2.name, s2.age, s2.subject)

Student.subject='python3' #change value of class variable

print(s1.name, s1.age, s1.subject)
print(s2.name, s2.age, s2.subject)

Example 6: Class or static variable.

class Library:
    org="Coding Academy"   #static or class variable
    def __init__(self):
        self.book = "Basics of Python" #instance variable
        self.date = "24-Feb-2022"

Library.org = "Python Academy"
lib1 = Library()
print("Issue Book:",lib1.book,"Issue Date:",lib1.date,"Organisation:",Library.org)

lib2 = Library()
lib2.book = "Advanced Python"
lib2.date = "1-Mar-2022"
print("Issue Book:",lib2.book, "Issue Date:",lib2.date,"Organisation",Library.org)