Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • I have used this effectively but with classes of my own instead of Python types. Given __init__(self, obj) I test inside __init__ with if str(obj.__class__.__name__) == 'NameOfMyClass': ... elif etc.. Commented Dec 30, 2019 at 22:40
  • This really isn't very Pythonic. __init__ should take a year and a quarter directly, rather than a single value of unknown type. A class method from_date can handle extracting a year and quarter from a datetime.date value, then calling YearQuarter(y, q). You could define a similar class method from_tuple, but that hardly seems worth doing since you could simply call YearQuarter(*t). Commented Jul 26, 2020 at 16:59
  • @chepner I gave it a huge update. Please tell me what you think. Commented Jul 27, 2020 at 10:16
  • It's still a mess (even more so than before) of special cases. __init__ shouldn't responsible for analyzing every possible set of values you might use to create an instance. def __init__(self, year, quarter): self._year = year; self._quarter = quarter: that's it (though may be with some range checking on quarter). Other class methods handle the job of mapping a different argument or arguments to a year and a quarter that can be passed to __init__. Commented Jul 27, 2020 at 14:18
  • For example, from_year_month takes a month m, maps it to a quarter q, then calls YearQuarter(y, q). from_date extracts the year and the month from the date instance, then calls YearQuarter._from_year_month. No repetition, and each method is responsible for one specific way of generating a year and a quarter to pass to __init__. Commented Jul 27, 2020 at 14:34