Message213305
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. |
|
Date |
User |
Action |
Args |
2014-03-12 21:55:00 | Giacomo.Alzetta | set | recipients:
+ Giacomo.Alzetta, docs@python |
2014-03-12 21:55:00 | Giacomo.Alzetta | set | messageid: <1394661300.29.0.784191218762.issue20902@psf.upfronthosting.co.za> |
2014-03-12 21:55:00 | Giacomo.Alzetta | link | issue20902 messages |
2014-03-12 21:55:00 | Giacomo.Alzetta | create | |
|