Ding ding ding. (This bit me as I had a recursive loading dependencies, detecting cycles with a set. I assumed |= would expand out to s = s | t, like the += operator with integers*. Oops.)
(* More trivia: i += 1 is not an atomic operation in CPython, but list.append is!)
July 10th, 2008 at 6:31 am
I don’t know. I cheated and tested it out, but I couldn’t see any difference in the resulting set a. Is one faster or something?
-Tyler S.
July 10th, 2008 at 7:05 am
As someone who doesn’t know Python all that well, what is the difference?
I tried on the command line and seemed to to get the same result.
July 10th, 2008 at 10:34 am
Do they use different internal methods to get their results?
July 10th, 2008 at 7:02 pm
__ior__
vs
__or__
The former is done in place, fancy that.
July 11th, 2008 at 12:53 am
Ding ding ding. (This bit me as I had a recursive loading dependencies, detecting cycles with a set. I assumed |= would expand out to s = s | t, like the += operator with integers*. Oops.)
(* More trivia: i += 1 is not an atomic operation in CPython, but list.append is!)
July 11th, 2008 at 7:42 am
I immeidately guessed it, though probably would have fallen for it myself:
July 11th, 2008 at 7:43 am
By the way, this is *exactly* why I still like good old C above all the other languages I write in. Every allocation made is one that I intended.
July 13th, 2008 at 12:22 pm
Funny corner cases like this make me think that set() should perhaps not overload operators quite so liberally.
The implementations of < and > are also counterintuitive:
assert not set([1]) > set([2])
assert not set([1]) < set([2])
July 14th, 2008 at 3:40 am
Is this really surprising? (Or have I been drinking the kool-aid so long that I forget what plain water is?)
Immutable types implement a += b as a = a + b, while mutable types make the operation in-place.
In place:
a = range(5)
a += [6]
New instance:
a = “five”
a += “six”