In a codebase I’m working on, the previous authors used this pattern of using a new() classmethod to create objects instead of __init__(). The __init__() methods are all very short and just set some default values while new() does the heavy lifting. This confuses me though because as far as I can tell, all the logic and work going on in new() would be right at home in __init__().
When would this
class Foo:
@classmethod
def new(cls, spam, eggs):
foo = Foo()
foo._bar = Bar.new(spam, eggs)
return foo
def __init__(self):
self._bar = None
be preferable to this?
class Foo:
def __init__(self, spam, eggs):
self._bar = Spam(spam, eggs)
Is this a pattern that anyone has seen before?