This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: collections.abc.Set doesn't provide copy() method
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: akira, gvanrossum, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-07-29 21:28 by akira, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
set-copy.patch akira, 2014-07-29 21:28 implement Set.copy(), mention it in Set's documentation, add test review
Messages (5)
msg224255 - (view) Author: Akira Li (akira) * Date: 2014-07-29 21:28
The documentation for standard types says [1]:

  clear() and copy() are included for consistency with the interfaces of
  mutable containers that don’t support slicing operations (such as dict
  and set)

  New in version 3.3: clear() and copy() methods.

[1] https://docs.python.org/3.4/library/stdtypes.html#immutable-sequence-types

but collections.abc documentation mentions only clear() method.

I've uploaded a patch that implements Set.copy() method, mentions it in
Set's documentation, and adds a sanity test.
msg224371 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-07-31 01:57
I don't think this is needed nor do I think that it is a good idea to have a copy() method in the ABCs because they know so little about their concrete underlying class (perhaps the backing store in the filesystem or a database).   Besides, a user already has workable alternatives such as creating a new instance and applying |= to populate it.

In general, we don't do API expansions without motivating use cases to guide the design.  

Also, note that MutableMapping does not have a copy method.
msg224373 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-31 02:23
Agreed with Raymond.  The return type of copy() for ABCs feels problematic.  MutableMapping doesn't have it either.
msg224403 - (view) Author: Akira Li (akira) * Date: 2014-07-31 13:15
> I don't think this is needed nor do I think that it is a good idea to
> have a copy() method in the ABCs because they know so little about
> their concrete underlying class (perhaps the backing store in the
> filesystem or a database).  Besides, a user already has workable
> alternatives such as creating a new instance and applying |= to
> populate it.

"because they know so little about their concrete underlying class"

FYI, the patch demonstrates how to implement copy() for *any*
MutableSet:

  def copy(self):
      return self._from_iterable(self)

_from_iterable() default implementation is cls(self) i.e., the copy()
method may be also efficient by default e.g., it may return the object
itself for immutable types.

---
The issue is closed so the rest can be ignored.

> In general, we don't do API expansions without motivating use cases to
> guide the design.

The reason to add Set.copy() is the same one as for set.copy() i.e., add
the equivalent of L[:] to mutable containers that don't support slicing
as the documentation excerpt in msg224255 explicitly says (copy()
doesn't modify its object so it can be added to immutable type).

General purpose language should provide an ability to copy a container.

In practice, people spell this operation as CustomSet(s) thus hardcoding
the type and ruining the flexibility provided by s.copy() duck-typing
(or copy.copy()).

The same could be said about MutableMapping (separate issue) e.g., merge
a, b dictionaries assuming no .copy() method:

  d = dict(a)
  d.update(b)

vs. the same with .copy():

  d = a.copy()
  d.update(b)

The latter may also work for a custom dict. The former is hardcoded to
produce a dict e.g., you have to reimplement it (and all the surrounding
code) for OrderedDict, SortedDict, Splay tree -based dict, etc. Luckily
projects such as blist, banyan that implement custom container types do
provide copy() method.

I don't see why we should make it harder to correctly implement/use
custom set/dict types.
msg224430 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-31 17:16
You need to learn when to give up. :-)

I wasn't the one who added a copy() method to other containers.  I personally despise almost all uses of "copying" (including the entire copy module, both deep and shallow copy functionality).  I much prefer to write e.g. list(x) over x.copy() -- when I say list(x) I know the type of the result.
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66299
2014-07-31 17:16:13gvanrossumsetmessages: + msg224430
2014-07-31 13:15:30akirasetmessages: + msg224403
2014-07-31 02:23:28gvanrossumsetmessages: + msg224373
2014-07-31 01:57:34rhettingersetstatus: open -> closed
versions: - Python 3.4
messages: + msg224371

assignee: rhettinger
resolution: rejected
2014-07-30 00:23:05pitrousetnosy: + serhiy.storchaka
2014-07-30 00:22:08pitrousetnosy: + gvanrossum, rhettinger
2014-07-29 21:28:19akiracreate