[Python-Dev] subclassing builtin data structures
Alexander Belopolsky
alexander.belopolsky at gmail.com
Fri Feb 13 17:55:48 CET 2015
On Thu, Feb 12, 2015 at 11:01 PM, Guido van Rossum <guido at python.org> wrote:
>
>> 2) always use the type of self when creating new instances
>> ..
>> cons:
>> - if constructor signatures change, must override all methods which
>> create new objects
>>
>
> Con for #2 is a showstopper. Forget about it.
>
Sorry if I am missing something obvious, but I still don't understand why
the same logic does not apply to class methods that create new instances:
>>> from datetime import *
>>> date.today()
datetime.date(2015, 2, 13)
>>> datetime.today()
datetime.datetime(2015, 2, 13, 11, 37, 23, 678680)
>>> class Date(date):
... pass
...
>>> Date.today()
Date(2015, 2, 13)
(I actually find datetime.today() returning a datetime rather than a date a
questionable design decision, but probably the datetime type should not
have been a subclass of the date to begin with.)
Are there any date subclasses in the wild that don't accept year, month,
day in the constructor? If you create such a class, wouldn't you want to
override __add__ and friends anyways? We already know that you will have
to override today().
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150213/56cd2127/attachment.html>
More information about the Python-Dev
mailing list