# Class - Template , Blueprints
# Objects - Instances of the class
# DRY - Do Not Repeat Yourself
class Employee:
no_of_leaves = 8 # class variable
# Constructer = Use to initialize objects
def __init__(self,name,salary,role,workinghrs):
self.name = name
self.salary = salary
self.role = role
self.workinghrs = workinghrs
# Method
def printdetails(self):
return f"The Name is {self.name},Salary is {self.salary},Role is {self.role} and Working Hours are {self.workinghrs}"
emp1 = Employee("EMP1",99999,"Senior Manager",11) # First Object
emp2 = Employee("EMP2",88888,"Manager",12) # Second Object
''' Both the objects have different memory location '''
print(emp1.printdetails())
print(emp2.printdetails())
''' The below method is used when we don't use Construter '''
# # Instance Variables #
# emp1.name = "EMP1" # You can take any name
# emp1.salary = 99999
# emp1.role = "Senior Manager"
# emp1.workinghrs = 11
# print(emp1.printdetails())
# emp2.name = "EMP2"
# emp2.salary = 88888
# emp2.role = "Manager"
# emp2.workinghrs = 12
# print(emp2.printdetails())
print(emp1.__dict__) # Returns no. of variables defined with the help of objects
print(Employee.no_of_leaves) # Class variable can be accessed by class
print(emp1.no_of_leaves) # Class variable can also be accessed by object
Employee.no_of_leaves = 9 # But class variable can be changed only by the class,not by the object
print(Employee.no_of_leaves)
emp1.no_of_leaves = 9
''' If you tried to changed class variable by the object
It will create new instance variable of that object '''
print(emp1.__dict__)
Comments
Post a Comment