Instance Variables: =================== Creation of Instance Variable using object reference of the class. ================================================================== Syntax: object-name = class-name() object-name.identifier = value # Instance Variables # Adding of instance variables to the class using an object reference class myClass: def met1(self): print("Hi") print("Good Morning.") print("Welcome To Ashok IT") obj1 = myClass() obj1.met1() print(obj1.__dict__) obj1.var1 = 1212 obj1.var2 = 9797 print(obj1.__dict__) Deletion of Instance Variable: ============================== using del property Syntax: (can help to delete an instance variable using object reference (from outside the class) del object-name.member-name(Instance-variable-name) ==> To delete an instance variable within the class, "self" can be used. Syntax: del self.instance-variable-name # Instance Variables # Adding of instance variables to the class using an object reference class myClass: def met1(self): self.a = 1993 self.p = 1997 print("Hi") print("Good Morning.") print("Welcome To Ashok IT") def met2(self): # for deleting the instance variables print("The Instance Variables are:") print(self.a) print(self.p) del self.a del self.p print("The Instance Variables are:") print(self.a) print(self.p) obj1 = myClass() obj1.met1() print(obj1.__dict__) obj1.var1 = 1212 obj1.var2 = 9797 obj1.var3 = 112233 print(obj1.__dict__) del obj1.var2 print(obj1.__dict__) obj1.met2() ==================================================== Static Variables: ================= class ICICI: def checkBalance(self): implementation print(balance) def homeLoan(): roi = 9% person-1 ==> ICICI ==> Phonepe/Paytm ==> 1245600.00 person-2 ==> ICICI ==> Phonepe/Paytm ==> 12300.00 person-1.homeLoan() ==> roi = 9% person-2.homeLoan() ==> roi = 9% ==> Static variables ==> Static variables can always define inside the static methods. ==> The static variables can able to access using either "self" or "class name". (preferred : class-name) Instance Variable Vs Static Variable: ===================================== When we have the static variable in class, the static variable can be shared among different objects of the same class with same data (same copy of data can be shared among all the objects of the class). When we have an instance variable in class, an instance variable can be share among different objects with different copy of the data. # Instance Variable Vs Static Variable class Test: a = 100 # static variable def __init__(self): self.b = 200 # instance variable t1 = Test() t2 = Test() print("t1 data : ",t1.a,t1.b) # Instance Variable print("t2 data : ",t2.a,t2.b) # Instance Variable # Modification on instance and static variables Test.a = 1000 # Static variable t1.b = 2000 # instance variable print("t1 data : ",t1.a,t1.b) print("t2 data : ",t2.a,t2.b) ======================================= Static variable can be created in: 1) within the class (without the self keyword) 2) within the constructor (with class name) 3) within the instance method (with class name) Syntax: class-name.variable-name = value 4) within the class method (with class name or cls variable name) Syntax: cls.variable-name = value 5) within the static method (using class name) Class Method: ============ ==> can define inside the class with a decorator (annotation) "@classmethod" ==> class method can take a class as an argument. Syntax: @classmethod def method-name(cls): implementation Use case: ======== class method can always use to define factory methods. Factory methods can always use objects in a systematic way. Phonepe: transfer(): checkBalanace(): checkBalance() ==> object Static Method: =============== static method can also define inside the class using an annotation/decorator "@staticmethod" Syntax: @staticmethod def meth-name(): implementation Use case: ========= working methods # Static variables class staticVariable: # defining the static variable at the beginning of the class a = 100 # static variable # staticVariable.b = 200 Incorrect def __init__(self): staticVariable.b = 200 # static variable def instanceMethod(self): staticVariable.c = 300 # static variable @classmethod def classMethod(cls): cls.d = 400 # static variable staticVariable.e = 500 # static variable @staticmethod def staticMethod(): staticVariable.f = 600 # static variable print(staticVariable.a) obj = staticVariable() print(staticVariable.b) # static variable of constructor can access only with class name # print(obj.b) not considered to access obj.instanceMethod() print(staticVariable.c) # print(obj.c) obj.classMethod() print(staticVariable.d) print(staticVariable.e) # print(obj.d) # print(obj.e) staticVariable.staticMethod() # obj.staticMethod() print(obj.f)