Editor

Create a Factory Method

Beginner · Write the code · from Python @classmethod vs @staticmethod: When to Use Each

From the lesson: Python @classmethod vs @staticmethod: When to Use Each

Imagine a company with three kinds of employees. Regular employees work on their own tasks (they need to know about themselves). Managers can hire new employees for the whole department (they need to know about the department).

Read the lesson

Create a Person class with name and age attributes. Add a @classmethod called from_birth_year that takes name and birth_year, calculates the age (use 2024 as the current year), and returns a new Person.

Create a person using Person.from_birth_year('Alice', 2000) and print their name and age on separate lines.

# Create your Person class here

More Classmethod and Staticmethod exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz