Static Variables: ================= Is it possible to access the static variable with object reference? ==================================================================== class staticVariables: a1 = 1212 # static variable def __init__(self): staticVariables.a2 = 123.234 # static variable def instance(self): staticVariables.a3 = True @staticmethod def static(a,b): staticVariables.a = a staticVariables.b = b @classmethod def classMethod(cls,a,b): cls.a = a staticVariables.b = b obj = staticVariables() print("First Value = ",obj.a1) print("Second Value =",obj.a2) obj.instance() print("Third Value = ",obj.a3) obj.static(112,121) print("Fourth Value = ",obj.a) print("Fifth Value = ",obj.b) obj.classMethod(112,121) print("Sixth Value = ",obj.a) print("Seventh Value = ",obj.b) Note: ===== We can allowed to access the static variables using the object reference and using class name. But the suggestable way to access the static variables and static methods is using class name. Do we able to modify the static variables values? ================================================= class modifyStatic: p = 1122 def instance(self): # to modify the static variable value inside the instance method # we can use "class name" modifyStatic.p = 1234 @staticmethod def static(): # to modify the static variable value inside the static method # we can use "class name" modifyStatic.p = 12e-7 @classmethod def classMethod(cls): cls.p = "Python" obj = modifyStatic() print("The Static Variable before the change = ",obj.p) obj.instance() print("The Static Variable after the change at Instance Method = ",modifyStatic.p) modifyStatic.static() print("The Static Variable after the change at Static Method = ",modifyStatic.p) modifyStatic.classMethod() print("The Static Variable after the change at CLass Method = ",modifyStatic.p) # Modification of static variable in outside the class. modifyStatic.p = 123-234j # # obj.p = 123.234j print("The Modified Static Variable from outside of the class = ",modifyStatic.p) # # obj.p = "Static" # print("The Modified Static Variable from outside of the class = ",modifyStatic.p) # obj.p = 112233 # print(obj.p) Note: ===== We can modify the static variable in inside the class and from outside the class also. When we can try to modify the static variable in outside the class using object reference, it may not be reflect to change. Because the object reference can add the new entry to the class as member from outside the class. So, it is always better follow the class name to modify the static variable from outside the class. Is it possible to delete the static variable? ============================================== delete the data inside the class: ================================ Syntax: del static-variable-name del class-name.static-variable-name ==> inside the instance and class, static method del cls.static-variable-name ==> for only class method delete the static variable in outside the class: ================================================= Syntax: del class-name.static-variable-name class deleteStatic: a = 1122 def instance(self): deleteStatic.b = True @staticmethod def statia(): deleteStatic.c = "TRUE" @classmethod def classMethod(cls): del cls.a obj = deleteStatic() print(obj.a) obj.instance() print(deleteStatic.b) deleteStatic.statia() print(deleteStatic.c) # deleteStatic.classMethod() # print(obj.a) del deleteStatic.b print(deleteStatic.b) =========================================== Static Methods: =============== ==> used to define utility methods ==> to define static method: @staticmethod we should use above the definition. Syntax: @staticmethod def method-name(arg1, arg2,...): implementation ==> static methods can be invoked using an object reference and also using class name. (Class name is suggestable) class Methods: ============== before the class method definition, we should add: @classmethod Syntax: @classmethod def method-name(cls,par1, par2,...): implementation Is it possible to return a value from static method and class method? ===================================================================== class returnValue: @staticmethod def met1(a,b): if a < b: small = a else: small = b return small @classmethod def met2(cls,a,b): result = a + b return result obj = returnValue() smallest_value = obj.met1(121,111) print(smallest_value) result = obj.met2(1234,4321) print(result) print(returnValue.met1(131,133)) print(returnValue.met2(112233,334455)) =================================================== Setter Methods and Getter Methods: ================================== Setter Method: ============== used to set the values to the instance variables also called as "mutator methods". Syntax: def setVariable(self,variable-name): self.variable-name = variable-value Note: ===== for every setter method there will one getter method. Getter Method: ============== used to get the values of instance variables. also called as "accessor methods" Syntax: def getVariable-name(self): return self.variable # setter methods class student: def setStudentName(self,sname): self.sname = sname def getStudentName(self): return self.sname def setStudentRoll(self,sroll): self.sroll = sroll def getStudentRoll(self): return self.sroll n = int(input("Enter number of students:")) for i in range(n): s = student() name = input("Enter name:") s.setStudentName(name) roll = int(input("Enter roll:")) s.setStudentRoll(roll) print("Hi",s.getStudentName()) print("Your Roll Number is:",s.getStudentRoll()) print()