Session Ready
Exercise

Alternative constructors

Python allows you to define class methods as well, using the @classmethod decorator and a special first argument cls. The main use of class methods is defining methods that return an instance of the class, but aren't using the same code as __init__().

For example, you are developing a time series package and want to define your own class for working with dates, BetterDate. The attributes of the class will be year, month, and day. You want to have a constructor that creates BetterDate objects given the values for year, month, and day, but you also want to be able to create BetterDate objects from strings like 2020-04-30.

You might find the following functions useful:

  • .split("-") method will split a string at"-" into an array, e.g. "2020-04-30".split("-") returns ["2020", "04", "30"],
  • int() will convert a string into a number, e.g. int("2019") is 2019 .
Instructions 1/2
undefined XP
  • 1
  • 2

Add a class method from_str() that:

  • accepts a string datestr of the format'YYYY-MM-DD',
  • splits datestr and converts each part into an integer,
  • returns an instance of the class with the attributes set to the values extracted from datestr.