[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-15 Thread Raymond Hettinger

Raymond Hettinger added the comment:

Thanks Terry.  

And yes, your reading of the set.update() docs is correct, Update the set, 
adding elements from all others means that it updates the set by adding the 
elements from the other sets.

FWIW, getting into details about which value wins goes against the core 
concept of the data structure.  Sets (in a mathematical sense) are about 
treating elements of an equivalence class as being identical.  We intentionally 
treat {1, 1.1} as being of length one and equal to {1.1, 1} eventhough the 
integer 1 and the float 1.0 are actually distinguishable in ways not tested by 
equality.

This isn't just a concept with sets.  The dict.update() method works similarly 
as does other container operations that depend on a notion of equivalence that 
is independent of other distinctive traits (object identity, attached values, 
type, etc).

--
resolution:  - invalid
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-14 Thread Terry J. Reedy

Terry J. Reedy added the comment:

Defining equality to ignore the .value attribute (and the id), says that they 
*do not matter*. Python believes you and the interpreter does what it does. If 
we had made a 'first or second operand wins' claim, we would not have been able 
to optimize intersection by starting with the smaller set. I may mis-remember 
but I believe we changed after someone noticed that something like {1]  
set(range(1)) versus set(range(1))  {1} had very different run times.

I agree with Raymond that the docs for intersection and union should be left as 
is.

I could read the doc for A.intersection_update(B) as saying that it keeps all 
items in A that also appear in B, with appear defined on the basis of ==, so 
that the result is a subset of the actual items in A. If that is the intent, it 
could be made clearer.

The doc A.update(B) (union) come close to saying that all items in A remain and 
items in B not in A get added, so that the result all the items in A. Again, if 
that is the intent, it could be made explicit, and people who want to determine 
which operand wins could use the update functions to do so.

--
nosy: +terry.reedy

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-13 Thread Giacomo Alzetta

Giacomo Alzetta added the comment:

I asked this because, for example, in Haskell it *is* a well-defined behaviour 
(see its documentation at: 
http://hackage.haskell.org/package/containers-0.5.4.0/docs/Data-Set.html): the 
left operand is preferred by all operations.

In fact, working with Haskell, I also have used such behaviour in the past. For 
example when writing a simple type-checker it's quite convenient to use such 
behaviour when handling environments where you want inner blocks to hide outer 
variables. Not defining such behaviour means that you must re-implement the 
wheel in order to achieve the correct behaviour.

In any case, this information should be made explicit in the docs, whether we 
want to make the behaviour defined or not.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-13 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
assignee: docs@python - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-13 Thread Raymond Hettinger

Raymond Hettinger added the comment:

 As far as I can tell currently there is no rule about this. 
 Intersection prefers the second operand, while union prefers the first.

The implementation uses the same logic as found in Lib/sets.py where the 
intersection operator loops over the SMALLER of the two input sets and does 
membership testing over the LARGER set.

 In any case, this information should be made explicit in the docs

Not really.  Historically, we've resisted the urge to over-specify or to 
declare something as undefined.  In general, we make affirmative guarantees 
when they are useful and when we're prepared to make sure the guarantee will 
always hold (for example, it took a long while before sorts were guaranteed to 
be stable).

Another consideration is that for most users, additional notes of behavior xxx 
is undefined is confusing, disconcerting, distracting, and rarely helpful.  
Language lawyers tend to request this sort of wording out of some sense of 
completeness, but it doesn't actually make the docs better for users.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread Giacomo Alzetta

New submission from Giacomo Alzetta:

Currently the documentation for set (at: 
http://docs.python.org/2/library/stdtypes.html#set) does not mention which 
operand is preferred when performing the usual binary operations.

For example the following sample code doesn't have a defined result according 
to the documentation:


class MyObj:
  def __init__(self, key, value):
 self.key = key
 self.value = value
  def __key(self):
return self.key
  def __hash__(self):
return hash(self.__key())
  def __eq__(self, other):
return type(self) is type(other) and self.__key() == other.__key()

a = {MyObj(1, 'a')}

b = {MyObj(1, 'b')}

print((a  b).pop().value)  # 'a' or 'b'?


As far as I can tell currently there is no rule about this. Intersection 
prefers the second operand, while union prefers the first. These are the only 
operations where this is important since they include common elements.

I believe that this kind of information ought to be included explicitly near 
such operations. At the very least, if the behaviour should really be somehow 
undefined, it should be stated there so that people don't rely on such 
behaviour.

The same should be stated for |= and =, since the first will not modify common 
elements while the latter will.


PS: I can't find any resource about the formatting of issues (e.g. if and which 
syntax is used for code blocks. Sorry for that.

--
assignee: docs@python
components: Documentation
messages: 213305
nosy: Giacomo.Alzetta, docs@python
priority: normal
severity: normal
status: open
title: Which operand is preferred by set operations? Missing information in the 
documentation
type: enhancement

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread R. David Murray

R. David Murray added the comment:

If you take the intersection or union of a set, it shouldn't matter which set 
the element came from, by the axioms that apply to sets.  So it if does, 
that's an application bug, IMO.

--
nosy: +r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread Éric Araujo

Changes by Éric Araujo mer...@netwok.org:


--
nosy: +stutzbach

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread R. David Murray

R. David Murray added the comment:

Or, to put it another way, which set it comes from is not documented because it 
is an implementation detail and can not be depended on.

I'm sure someone will correct me if I'm wrong :)

--
nosy: +rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread Steven D'Aprano

Steven D'Aprano added the comment:

If this is undefined, and I think it should be, the docs should explicitly say 
so. How is this?

The union and intersection operations select elements which appear in both 
operands, e.g. set([a, b, c])  set([c, d, e]) returns set([c]). The selection 
is performed by equality, not object identity, and which specific object is 
returned (the c object from the first set or the second set) is an 
implementation detail and cannot be relied on.

--
nosy: +stevenjd

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread R. David Murray

R. David Murray added the comment:

Sounds good to me.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread Balakrishnan B

Changes by Balakrishnan B balakrishnan.er...@gmail.com:


--
nosy: +Balakrishnan.B

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20902] Which operand is preferred by set operations? Missing information in the documentation

2014-03-12 Thread Benjamin Peterson

Changes by Benjamin Peterson bp+pyb...@benjamin-peterson.org:


--
keywords: +easy

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20902
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com