[Python-Dev] Return type of alternative constructors (original) (raw)
Guido van Rossum guido at python.org
Sun May 8 18:50:10 EDT 2016
- Previous message (by thread): [Python-Dev] Return type of alternative constructors
- Next message (by thread): [Python-Dev] Return type of alternative constructors
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, May 8, 2016 at 4:49 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
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)
Honestly, either of these sounds like fragile, even though I really want the alternative constructor to return an instance of the subclass (else why invoke it through the subclass).
> 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.
Putting this constraint in the docs sounds fragile too. :-(
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.
Indeed!
We could also consider this a general weakness of the "alternative constructors are class methods" pattern. If instead these alternative constructors were folded into the main constructor (e.g. via special keyword args) it would be altogether clearer what a subclass should do.
-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20160508/ad7d89f3/attachment.html>
- Previous message (by thread): [Python-Dev] Return type of alternative constructors
- Next message (by thread): [Python-Dev] Return type of alternative constructors
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]