OOPs Part_03 ============ Instance Variables: =================== ==> also called as "Object level variables" ==> the variables of the class can differ from object to object are called as "Instance Variables". ==> Instance variables always access with/through object only. Ex: class MyClass: def __init__(self,a,b): self.a = a self.b = b mc1 = MyClass(100,200) mc2 = MyClass(1000,2000) print("a = ") print(mc1.a) print(mc2.a) print("b = ") print(mc1.b) print(mc2.b) Where we can declare Instance Variables: ======================================== class MyClass: # instance variables can declare inside the constructor def __init__(self): self.name = "Rajesh" self.age = 24 # we can define in the class method def definitionMethod(self): self.a = 123 # local variable self.b = 123-23.4j # we can add instance variable to the class from outside the class also by using the object. mc1 = MyClass() mc1.p = 1234 mc1.mobile = 8977029779 print("The Data of the class is = ") print(mc1.name,mc1.age) mc1.definitionMethod() print(mc1.a,mc1.b) print(mc1.p) print(mc1.mobile) Note: ===== When the local variable and instance variable with same name, there is a name conflict. ==> To overcome this, instance variable always define with "self" keyword. class Car: def __init__(self,brand,color, maxSpeed): self.brand = brand self.color = color self.maxSpeed = maxSpeed def displayDetails(self): print(f"Brand Name : {self.brand},Color of the Brand : {self.color} and Maximum Speed : {self.maxSpeed}\n") car1 = Car("Toyota","Red",180) car2 = Car("Honda","Blue",200) car1.displayDetails() car2.displayDetails() =============================================================== Static Variables: ================= ==> also called as "Class Level Variables". ==> can always access with the class name or object name but prefer with class name only. ==> The value of static variable can be fixed from object to object. Note: ==== No instance variable inside the class can define directly (without the constructor/method). But we can define the static variable inside the class directly without any keyword like "self" or class name. class MyClass: # static variable can define in the class directly # self.name = "Ravi" # instance variable age = 30 # static variable # define the static variable inside the constructor. def __init__(self): MyClass.a = 1221 # static variable can also define inside the method def method(self): MyClass.x = "Python is Awesome" @staticmethod def staticMethod(): MyClass.p = "Python" # self.p1 = 1122 @classmethod def classMethod(cls): MyClass.d = 101 cls.e = 121 # self.e1 = 11223 mc = MyClass() # print(mc.name) print(MyClass.age) print(MyClass.a) print(mc.a) mc.method() print(MyClass.x) MyClass.staticMethod() print(MyClass.p) # print(self.p1) MyClass.classMethod() print(MyClass.d) print(MyClass.e) # print(self.e1) =================================================== class Employee: employeeCount = 0 # static variable def __init__(self,name,position): self.name = name self.position = position Employee.employeeCount += 1 def displayDetails(self): return f"Name : {self.name} Position : {self.position}" emp1 = Employee("Dinesh","Auotmation Test Engineer") emp2 = Employee("Keerthana","Software Engineer") emp3 = Employee("Rakesh","Sr.Software Developer") print(emp1.displayDetails()) print(emp2.displayDetails()) print(emp3.displayDetails()) print("The Total Number of Employees = ",Employee.employeeCount) ================================================================ Static Method: ============= ==> static method is class level method ==> we can call the static method using the class name only. Ex: BankingApplication: withdraw: same for any type of customer Savings Customer Current Customer class DiscountCalculation: # static variable discountRate = 0.10 # static method @staticmethod def calculateDiscountedPrice(originalPrice): if originalPrice < 0: raise ValueError("Price Cannot be Negative.") return originalPrice - (originalPrice * DiscountCalculation.discountRate) originalPrice = 1000 discountedPrice = DiscountCalculation.calculateDiscountedPrice(originalPrice) print("The Discounted Price = ",discountedPrice) ==================================================================== Class Method: ============ class Car: totalCars = 0 def __init__(self,brand, model, year): self.brand = brand self.model = model self.year = year @classmethod def fromString(cls,carInfo): brand, model, year = carInfo.split(",") cls.totalCars+=1 return cls(brand,model,int(year)) @classmethod def carsProduced(cls): return f"Total Cars produced : {cls.totalCars}" car1 = Car.fromString("Toyota,Corolla,2023") car2 = Car.fromString("Honda, Civic,2024") print(Car.carsProduced()) print(f"Car1:{car1.brand} {car1.model} {car1.year}") print(f"Car2:{car2.brand} {car2.model} {car2.year}")