[Python-Dev] The type of the result of the copy() method
Guido van Rossum
guido at python.org
Sun Oct 29 13:04:30 EDT 2017
It's somewhat problematic. If I subclass dict with a different constructor,
but I don't overload copy(), how can the dict.copy() method construct a
correct instance of the subclass? Even if the constructor signatures match,
how can dict.copy() make sure it copies all attributes properly? Without an
answer to these questions I think it's better to admit defeat and return a
dict instance -- classes that want to do better should overload copy().
I notice that Counter.copy() has all the problems I indicate here -- it
works as long as you don't add attributes or change the constructor
signature. I bet this isn't documented anywhere.
On Sun, Oct 29, 2017 at 9:40 AM, Brett Cannon <brett at python.org> wrote:
> It probably should be more consistent and I have a vague recollection that
> this has been brought up before.
> On Sun, Oct 29, 2017, 08:21 Serhiy Storchaka, <storchaka at gmail.com> wrote:
>> The copy() methods of list, dict, bytearray, set, frozenset,
>> WeakValueDictionary, WeakKeyDictionary return an instance of the base
>> type containing the content of the original collection.
>> The copy() methods of deque, defaultdict, OrderedDict, Counter,
>> ChainMap, UserDict, UserList, WeakSet, ElementTree.Element return an
>> instance of the same type as the original collection.
>> The copy() method of mappingproxy returns a copy of the underlying
>> mapping (using its copy() method).
>> os.environ.copy() returns a dict.
>> Shouldn't it be more consistent?
>> Python-Dev mailing list
>> Python-Dev at python.org
>> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> Python-Dev mailing list
> Python-Dev at python.org
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev