Class Method As Alternative Constructer

# Class Method As Alternative Constructer #
# Class Method - Which takes class as argument #

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}"

@classmethod # Decorator #
def change_leaves(cls,newleaves):
cls.no_of_leaves = newleaves

@classmethod
def from_slash(cls,str): # Class Method As Alternative Constructer #
emp3_list = str.split("/") # split function will split the string from the slash and it will create a list #
print(emp3_list)
return cls(emp3_list[0],emp3_list[1],emp3_list[2],emp3_list[3])

emp1 = Employee("EMP1",99999,"Senior Manager",8) # First Object
emp2 = Employee("EMP2",88888,"Manager",9) # Second Object
emp3 = Employee.from_slash("EMP3/55555/Junior Manager/11")
print(emp3.salary)

Comments

Popular posts from this blog

Python Program To Store Marks

Currency.txt

Python Comprehensions