class Student:
def __init__(self, first, last):
self.first = first
self.last = last
@classmethod
def from_string(cls, emp_str):
first, last = emp_str.split("-")
return cls(first, last)
Student_1 = Student("Cool", "Person")
Student_2 = "Another-One"
Student_2 = Student.from_string(Student_2)
Why is return used in this class method? I know you need it to work. But I'm not able to wrap my mind around why you need to include it. From what I know - in this example in the classmethod, cls(first, last)
is doing the same thing as __init__(self, first, last)
. But why the need to include a return
in there? Shouldn't just cls(first, last)
be enough to call the __init__
, which is what you already do when constructing the instance like Student_1
?
Can you explain where my confusion lies?