Noam Raphael wrote:
> Perhaps it bothers the programmer with something that shouldn't bother
> him. I mean that I might do help(set.copy), and then think, "Oh, it
> returns a shallow copy. Wait a minute - 'shallow' means that I get a
> new object, which references the same objects as the old one. Perhaps
> I should use another function, which does deep copying? Let me think
> about it - no. All members of a set are immutable, so it doesn't
> matter." I think that in this case, the fact that the copy is shallow
> is an implementation detail, since there's no sense in making a deep
> copy of a set.

If "makes no sense" means "would not make a difference", then you
are wrong. Objects in a set are not necessarily unmodifiable;
they just have to be hashable. Watch this:

>>> class A:
...   def __hash__(self):return 0
...   def __cmp__(self, other):return 0
...
>>> a1 = A()
>>> import copy
>>> a2 = copy.deepcopy(a1)
>>> a1.x = 10
>>> a2.x = 20
>>> a1.x
10
>>> s = set([a1])
>>> a2 in s
True

So even though a1 and a2 are distinct objects with different, modifiable
state, they are treated as equal in the set. A deepcopy of the set (if
it existed) would do something different than a plain copy.

> I know it's just a word, and that it doesn't matter a lot. But why not
> make things a tiny bit better?

Things would get worse. The code would become underspecified.

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to