[issue25535] collections.Counter methods return Counter objects instead of subclass objects

Mark Dickinson report at bugs.python.org
Mon Nov 2 14:13:51 EST 2015


Mark Dickinson added the comment:

Relevant post from Tim Peters back in 2005, on the subject of subclassing datetime and timedelta:

"""
Yes, and all builtin Python types work that way.  For example,
int.__add__ or float.__add__ applied to a subclass of int or float
will return an int or float; similarly for a subclass of str.  This
was Guido's decision, based on that an implementation of any method in
a base class has no idea what requirements may exist for invoking a
subclass's constructor.  For example, a subclass may restrict the
values of constructor arguments, or require more arguments than a base
class constructor; it may permute the order of positional arguments in
the base class constructor; it may even be "a feature" that a subclass
constructor gives a different meaning to an argument it shares with
the base class constructor.  Since there isn't a way to guess, Python
does a safe thing instead.
"""

Source: https://mail.python.org/pipermail/python-list/2005-January/311610.html

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25535>
_______________________________________


More information about the Python-bugs-list mailing list