Source Code:
(back to article)
class MyClass: def __init__(self, name, age): self.name = name self.age = age @classmethod def from_string(cls, string): name, age = string.split(',') return cls(name, age) person1 = MyClass('John', 30) print(person1.name, person1.age) person2 = MyClass.from_string('Jane, 25') print(person2.name, person2.age)
Result:
Report an issue