OOPS in python

 Varaiable types inside class: here and here

Two types of variables:

Class or staic variables

Instance variables

 Types of methods inside class: here

3 types of methods:

Class methods

Instance methods 

Static methods


---------------

import and __main__ (here)


------------------

class or static variables:

  • If we want to change class or static variable, it will be changed for that specific instance or object of calss
  • If we want to upadte class varaible only for specific instance,we will do so as shown in below example or else if we  want to update class variable across all objects we need to use class method
Example:

class HelloWorld:

    name="bmaddi"   


obj=HelloWorld()

print(obj.name)  //bmaddi

obj.name="chand"

print(obj.name) .//chand

print(HelloWorld.name) //bmaddi

obj2=HelloWorld()

print(obj2.name) //bmaddi

---------------------------------------------------------

Class Method:(Best video)

  • If we want to update class variables we use class method
  • we can invoke calss method using object reference and class name,but it is best practice to use class name
Usecase 1:(chapter11 in harryExercies)

# Use case 1 of class method : to count objects in the

class Student:
    count=0
    def __init__(self,name,age):
        self.name = name
        self.age = age
        #Increment count by 1
        Student.count=Student.count+1
       
       
    def printInfo(self):
        print(f"Name of the student is {self.name} and his age is {self.age}")
       
    @classmethod    
    def objCount(cls):
        return cls.count
               

#create objects of class
stud1=Student("bhuvan",25)
stud2=Student("chand",24)
stud3=Student("chand",23)

#callin by class name
print(Student.objCount())       #3

#we can also call by an y object reference

print(stud2.objCount())   #3
print(stud3.objCount())   #3

#The reason behind getting 3 as output for every object is..Once objects are created __init__ constructor will be invoked, which increments the count of class variable by 3..Since it is not bound any class instance of class we will get 3






Usecase 2:

#In this use case, we will see increment base price of car every year based on inflation

class Car:
    basePrice=1000
    def __init__(self,model,color):
        self.model = model
        self.color = color
       
    @classmethod
    def increaseBasedOnInflation(cls,inflation):
        cls.basePrice = cls.basePrice+cls.basePrice*inflation  
       

car1 = Car("Suzuki","black")
car2 = Car("swift","white")
#Initial base price
print(car1.basePrice)

#increases base price by 1%, when we call the same from the next time we will get updated value for all objects
Car.increaseBasedOnInflation(0.1)
print(car1.basePrice)

Car.increaseBasedOnInflation(0.1)
print(car1.basePrice)

#class varaible value increased 1 percent every time we ivoked Car.increaseBasedOnInflation function,we cal also call same method using object reference but it is best practice to call using class name







         



Comments