Python Forum
Why is the copy method name in python list copy and not `__copy__`?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the copy method name in python list copy and not `__copy__`?
#1
Why is the name of copy method in python list copy and not __copy__?

As a result, I confirmed that separate exception handling logic was written for list (or dict, set, bytearray) inside the copy module.
#  https://github.com/python/cpython/blob/main/Lib/copy.py
def copy(x):
    cls = type(x)

    copier = _copy_dispatch.get(cls) # Code for a list that does not have a __copy__ method.
    if copier:
        return copier(x)

    # ...

    copier = getattr(cls, "__copy__", None)
    if copier is not None:
        return copier(x)

    # ...

_copy_dispatch = d = {}

# ...

d[list] = list.copy
This also applies to the __deepcopy__ method. (Even though list has a list.copy (not __copy__) method, there is no list.deepcopy method, so the copy module implements it.)

def _deepcopy_list(x, memo, deepcopy=deepcopy):
    y = []
    memo[id(x)] = y
    append = y.append
    for a in x:
        append(deepcopy(a, memo))
    return y
d[list] = _deepcopy_list
I am curious about the following:

Why is the copy method name in list copy and not __copy__?
Why is the deepcopy logic of list implemented in the copy module and not list.__deepcopy__?
I think that if list had __copy__ and __deepcopy__ methods, the exception handling code in the copy module would go away and consistency would be maintained. But there must be a reason why that wasn't done, and I'm curious as to why?
Reply
#2
How would __copy__() be called? There is no copy operator, nor is there an implicit need like __str__() used when printing an object.
Reply
#3
(Apr-04-2024, 01:10 AM)deanhystad Wrote: How would __copy__() be called? There is no copy operator, nor is there an implicit need like __str__() used when printing an object.

It can be called through the copy module. Of course, I'm not sure if that's an elegant way to do it, and I'm not dissatisfied with having a method named copy on the list.
Because list copy is a frequently used function, I agree if it is implemented as a list method without a copy module.
However, I was wondering why this (inconsistent) difference occurred because other data structures (such as array) have a __copy__ method rather than a copy method.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy from FTP(12.50.100.21) to FTP(12.45.222.25) anna17 0 263 Apr-09-2024, 01:44 PM
Last Post: anna17
  Copy xml content from webpage and save to locally without special characters Nik1811 14 1,116 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  Dataframe copy warning sawtooth500 4 445 Mar-25-2024, 11:38 PM
Last Post: sawtooth500
  Copy Paste excel files based on the first letters of the file name Viento 2 503 Feb-07-2024, 12:24 PM
Last Post: Viento
  Why can't I copy and past only ONE specific tab? NewWorldRonin 8 935 Jan-12-2024, 06:31 PM
Last Post: deanhystad
  How to copy work sheet data one workbook to other? sayyedkamran 2 733 Nov-03-2023, 09:10 AM
Last Post: Larz60+
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 807 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Fix pandas copy/slice warning. deanhystad 3 905 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Copy List Not Copying BAbdulBaki 3 681 Aug-19-2023, 02:03 AM
Last Post: perfringo
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,175 Jun-06-2023, 05:49 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020