INSTANCE VARIABLES: =================== ACCESSING OF INSTANCE VARIABLES: ================================ ==> POSSIBLE ONLY WITH OBJECT REFERENCE VARIABLE. Syntax: object-reference.instance-variable-name # Accessing of Instance Variables class instanceVariables: def __init__(self): self.a = 1221 self.b = 1234 def m1(self): self.c = 97 self.d = 79 iv = instanceVariables() print(iv.a) print(iv.b) iv.m1() print(iv.c) print(iv.d) =================================================== Static Variables: ================= The values of variables are not getting changed from object to object are called as "static variables". the static variable can create an object only one time and the same copy of static variable can be shared among different objects. ex: o1 ==> a = 123 o2 ==> a = 123 ==> to define the static variable, we can use: 1) using the class name Syntax: class-name.static-variable-name 2) without the class name, we can define the static variable inside the class but not in method and not in constructor. Syntax: static-variable-name = value ==> The Static variable can define: 1) Inside the constructor 2) Inside the method 3) Inside the class but outside to the class method. ==> To access the static variable, we can use: 1) Class name Syntax: class-name.static-variable-name 2) Object reference Syntax: object-reference.static-variable-name # Static Variable class staticVariables: p = 2000 q = 4000 def __init__(self): staticVariables.a = 100 staticVariables.b = 200 def m1(self): staticVariables.c = 300 staticVariables.d = 400 sv = staticVariables() # accessing static variables using class name print(staticVariables.p) print(staticVariables.q) print(staticVariables.a) print(staticVariables.b) sv.m1() print(staticVariables.c) print(staticVariables.d) # accessing of static variables with object reference print(sv.p);print(sv.q);print(sv.a);print(sv.b);print(sv.c);print(sv.d) ================================= Instance Variable Vs Static Variables: ====================================== The Copy of Instance Variables can be differ from object to object. The copy of Static variables can be same among multiple objects. # Instance Variable Vs Static Variable class test: def __init__(self): self.a = 100 self.b = 200 def m1(self): test.c = 1000 test.d = 2000 t1 = test() t2 = test() t1.m1() print("t1 data:",t1.a,t1.b,t1.c,t1.d) t2.m1() print("T2 data:",t2.a,t2.b,t2.c,t2.d) # modifying of instance and static variables t1.b = 1234 # instance variable modification test.c = 9779 print("t1 data:",t1.a,t1.b,t1.c,t1.d) print("T2 data:",t2.a,t2.b,t2.c,t2.d) ================================================== 3) Method Variables: ==================== also called as "local variables". # Scope of Instance and Static Variables # Method Variables class Test: def __init__(self): self.a = 121 Test.b = 123 def m1(self): x = 100 y = 200 print("Instance Variable value = ",self.a) print("Static Variable Value = ",Test.b) print("Local/Method Variables are = ",x,y) # def m2(self): # print("Local/Method Variables are = ", x, y) t = Test() t.m1() t.m2() ===================================== Types of Methods: ================ three types: 1) Instance Methods ==> The method with instance variables 2) Static Methods ==> The method with static variables 3) Class methods ==> the data of class methods can define with "cls"