[Python-Dev] subclassing builtin data structures
Ethan Furman
ethan at stoneleaf.us
Fri Feb 13 04:41:22 CET 2015
On 02/12/2015 06:39 PM, Alexander Belopolsky wrote:
> In my view, a constructor is no different from any other method. If the designers of the subclass decided to change the
> signature in an incompatible way, they should either override all methods that create new objects or live with tracebacks.
> On the other hand, if all I want in my Date class is a better __format__ method, I am forced to override all operators
> or have my objects silently degrade [...]
So there are basically two choices:
1) always use the type of the most-base class when creating new instances
pros:
- easy
- speedy code
- no possible tracebacks on new object instantiation
cons:
- a subclass that needs/wants to maintain itself must override all
methods that create new instances, even if the only change is to
the type of object returned
2) always use the type of self when creating new instances
pros:
- subclasses automatically maintain type
- much less code in the simple cases [1]
cons:
- if constructor signatures change, must override all methods which
create new objects
Unless there are powerful reasons against number 2 (such as performance, or the effort to affect the change), it sure
seems like the nicer way to go.
So back to my original question: what other concerns are there, and has anybody done any benchmarks?
--
~Ethan~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150212/0e71d90f/attachment.sig>
More information about the Python-Dev
mailing list