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

Reply via email to