Class methods as an alternative Constructor

 


class Employee:
no_of_leaves = 8

def __init__(self,aname,asalary,arole,occu):
self.name=aname
self.salary=asalary
self.role=arole
self.occupation=occu


def printdetails(self):
return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"

@classmethod
def change_leave(cls,leave):
cls.no_of_leaves=leave

@classmethod
def from_dash(cls,string):
# ks=string.split("-")
# print(ks)
# return cls (ks[0],ks[1],ks[2],ks[3])
return cls(*string.split('-'))




harry = Employee("Harry", 2555, "Instructor","Businessmen")
rohan=Employee("Rohan",4448,"student","Businessmen")



kartik=Employee.from_dash("kartik-8996-student-Businessmen")
print(kartik.occupation)

Comments

Popular posts from this blog

React Installation with Vite and Tailwind css (Steps to Installation)

Insert Data In mongoDb