[Python-Dev] Return type of alternative constructors

Nick Coghlan ncoghlan at gmail.com
Sun May 8 07:49:20 EDT 2016


On 8 May 2016 at 08:39, Serhiy Storchaka <storchaka at gmail.com> wrote:
> Should alternative constructor call __new__ and __init__ methods? Thay can
> change signature in derived class.

I think this is typically the way to go (although, depending on the
specific type, the unpickling related methods may be a more
appropriate way for the alternate constructor to populate the instance
state)

> Should it complain if __new__ or __init__ were overridden?

If there are alternate constructors that depend on either the
signature of __new__/__init__, unpickling support, or some other
mechanism for creating new instances, this should be mentioned in the
class documentation as a constraint on subclasses - if subclasses
don't want to meet the constraint, they'll need to override the
affected alternate constructors.

Cheers,
Nick.

P.S. The potential complexity of that is one of the reasons the design
philosophy of "prefer composition to inheritance" has emerged -
subclassing is a powerful tool, but it does mean you often end up
needing to care about more interactions between the subclass and the
base class than you really wanted to.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list