Class method as alternative constructors in python
#
class Employee:
profession = "Networking"
def __init__(self, aname, asalary, arole):
self.name = aname
self.role = arole
self.salary = asalary
def printdetails(self):
return F"Name is {self.name} Salary is {self.salary} and role is {self.role}"
@classmethod
def change_profession(cls, newprofessional):
cls.profession = newprofessional
@classmethod
def from_str(cls, string):
# params = string.split("-")
# print(params)
# return cls(params[0],params[1],params[2])
# how to perform this in one line
return cls(*string.split("/"))
umair = Employee("Umair", 344, "Instructor")
Comments
Post a Comment