Class methods as alternative constructor

class Employee:
no_of_leaves = 8

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

@classmethod
def from_str(cls, string):
'''params=string.split("-")

return cls(params[0],params[1],params[2])'''
return cls(*string.split("-"))


karan = Employee.from_str("karan-25000-datascientist")

print(karan.salary)

Comments

Popular posts from this blog

Steps taken by a compiler to execute a C program

Tokens in C

Variables and Data Types in C