classmethod()
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_string(cls, person_str):
name, age = person_str.split(",")
return cls(name, int(age))
person = Person.from_string("John,30")
print(person.name) # Выведет "John"
print(person.age) # Выведет 30class Circle:
pi = 3.14159
def __init__(self, radius):
self.radius = radius
@classmethod
def get_pi(cls):
return cls.pi
@classmethod
def create_unit_circle(cls):
return cls(1)
unit_circle = Circle.create_unit_circle()
print(unit_circle.radius) # Выведет 1
print(Circle.get_pi()) # Выведет 3.14159Последнее обновление