Instance Variables ================== # Instance variables can always define inside the class without of self keyword # Instance variables can also define inside the methods (Insatnce methods) with self keyword. # if an instance variable defined inside the class but above the methods, the instance variables # can extend to use in any where of the class. Even inside the method we can access using # self keyword. # When an instance variable defined in an instance method, we cannot use outside method (local scope) # But that variable can always access with object name. class MyClass: a = 121 b = 101 def instanceMethod(self): self.p = 1001 # instance variable self.q = 1221 x = 1122 y = 1234 # local variables/method variables print(self.a) # print(x) # print(self.p) print(a) mc = MyClass() print(mc.a) print(mc.b) mc.instanceMethod() # print(mc.x) print(mc.p) # print(MyClass.p) =================================== # Is it possible to define the instance variable in static method. # ans: No, we cannot create an instance variables inside the satatic method. class MyClass: @staticmethod def method(): self.a = 121 self.b = 131 mc = MyClass() mc.method() print(mc.a) print(mc.b) =========================================== Static Variables ================ # static variable always allowed to define with class name. # static variable always allowed to define inside the static method. # static variable can possible to define inside of the instance method also. class Practice: # Practice.name = "Ravi" # static variable @staticmethod def method(): Practice.name = "Ravi" # static variable def method1(self): Practice.age = 30 p = Practice() Practice.method() print(Practice.name) print(p.name) p.method1() print(Practice.age) ================================================== Constructor =========== -> When we want to send data to the class at the time of object creation, we need the constructor. -> The constructor is a special method. It can always allowed to define with "def" keyword. -> The constructor can be invoked by itself at the time of object creation. -> The name of constructor is "__init__" class MyClass: def __init__(self,name,age): self.name = name self.age = age def printing(self): print("Name = ",self.name) print("Age = ",self.age) mc = MyClass("Ajay",28) mc.printing() -> The constructor in python is of two types: 1) Parameter less constructor ============================ class MyClass: def __init__(self): self.name = "Ajay" self.age = 28 def printing(self): print("Name = ",self.name) print("Age = ",self.age) mc = MyClass() mc.printing() 2) Parameterized constructor -> When a class with two constructors, the earlier constructor definition can be get overridden with new constructor definition. To overcome this, we can be allowed to define only one constructor in the class. class MyClass: def __init__(self): self.name = "Ajay" self.age = 28 def __init__(self): print("Hello") def printing(self): print("Name = ",self.name) print("Age = ",self.age) mc = MyClass() mc.printing() =================================================== Class Method ============ Class Variables class MyClass: @classmethod def method(cls): # self.a = 123 # instance variable MyClass.b = 101 # static variable cls.c = 1122 # class variable print("Good night!") @staticmethod def m1(): cls.p = 1203 mc = MyClass() mc.method() MyClass.method() # print("Instance variable = ",mc.a) print("Static variable = ",MyClass.b) print("Class Variable = ",MyClass.c) print(mc.c) MyClass.m1() print(MyClass.p)