Class Method As Alternative Constructors

class Employee:
no_of_leaves = 89

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

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

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

@classmethod
def from_str(cls, string):
return cls(*string.split("-"))


harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")
karan = Employee.from_str("Karan-480-Student")

print(karan.printdetails())

Comments

Popular posts from this blog

Practice Execise 3

Pickle Module