OOPs Concepts: ============== Constructor: ============ 1) always can define inside the class only. 2) __init__ 3) can always define with keyword: "def" like a method. 4) method can be named with any identifier whereas the constructor can be named with only "__init__". 4) method can be invoked using an object to execute the block of code. Whereas the constructor can be invoked automatically at the time of object creation. # constructor class myClass: # constructor without parameters def __init__(self): print("Hi") print("Hello") print("Good Evening.") def method(self): print("Good Night.") mc = myClass() # Object creation mc.method() 5) If the constructor with more than one definitions, then the constructor can be overwritten with latest definition. # constructor class myClass: # constructor without parameters def __init__(self): print("Hi") print("Hello") print("Good Evening.") def method(self): print("Good Night.") def __init__(self): print("Hello all. Welcome to Python's OOP Concepts.") mc = myClass() # Object creation mc.method() ================================================ 6) Like methods, the constructor can accept parameters. # Constructor with parameters class Employee: def __init__(self,eid,ename,esal): print("The Employee Name = ",ename) print("The Employee Id =",eid) print("The Employee Salary = ",esal) emp = Employee(110,"Avinash",150000) 7) The Constructor can always return "None". =================================== Differences Between Constructor and Method: =========================================== 1) Method can define with any identifier constructor can always define with "__init__" only. 2) Method can return anything constructor can return "None" 3) Method can allow to define the business logic of the application constructor can use to define the definition of data only. # Constructor Vs Method class myClass: def __init__(self): self.a = 100 self.b = 200 self.c = 300 def findBig(self): if self.a > self.b and self.a > self.c: bigger = self.a elif self.b > self.c: bigger = self.b else: bigger = self.c return bigger mc = myClass() result = mc.findBig() print("The Biggest number = ",result) 4) Constructor can be invoked automatically at the time of object creation method need to be invoke using an object of the class. ============================================= Types of Variables: =================== three types: 1) Instance Variables (Object Level Variables) 2) Static Variables (Class Level Variables) 3) Method Variables (Method Level Variables/Local Variables) 1) Instance Variables ===================== The value of the variable can be varied from object to object, these variables are called "Instance Variables". Instance Variables can de declared in: 1) Inside the Constructor 2) Inside the Method 3) Using an Object of the class 1) Inside the Constructor ========================= ==> to define the instance variables inside the constructor, we can use "self" keyword. # Instance Variable in Constructor class Employee: def __init__(self): # Instance Variables definition self.eid = int(input("Enter Empolyee Id:")) self.ename = input("Enter Employee Name:") self.erole = input("Enter Employee Role:") self.esal = int(input("Enter Employee Salary:")) def printData(self): print("The Employee Details:") print("Id = ",self.eid) print("Name = ",self.ename) print("Role = ",self.erole) print("Salaray = ",self.esal) emp = Employee() emp1 = Employee() emp.printData() emp1.printData() ============================= 2) Inside the Method ==================== # Instance Variables in methods class Test: def __init__(self): self.a = 11 self.b = 21 def testMethod(self): self.c = 31 self.d = 41 def printMethod(self): print("The Instance Variables are:") print(self.a,self.b,self.c,self.d) t = Test() t.testMethod() t.printMethod() ========================================= Using an Object of the class ============================= # Instance Variables in methods class Test: def __init__(self): self.a = 11 self.b = 21 def testMethod(self): self.c = 31 self.d = 41 def printMethod(self): print("The Instance Variables are:") print(self.a,self.b,self.c,self.d) t = Test() t.testMethod() t.printMethod() print(t.__dict__) t.e = 51 print(t.__dict__)