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

Giacomo Alzetta report at bugs.python.org
Wed Mar 12 22:55:00 CET 2014


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 at python
components: Documentation
messages: 213305
nosy: Giacomo.Alzetta, docs at python
priority: normal
severity: normal
status: open
title: Which operand is preferred by set operations? Missing information in the documentation
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20902>
_______________________________________


More information about the docs mailing list