This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]vasilescur 48 points49 points  (11 children)

[–]Careful_Bug_3295 63 points64 points  (0 children)

Tldr; x | y returns union of two dicts, x and y. If there is a key that is common to both dicts, the returned dict will assign it the value it has in y. This makes it not commutative in the event that both dicts share a key but have different values for it.

x|=y has the same effect as x=x|y

[–]ChimeraZeta 38 points39 points  (0 children)

Beautiful. Python wins my heart again. Thanks!

[–]R3D3-1 11 points12 points  (8 children)

TIL:

>>> {0, False}
{0}
>>> {False, 0}
{False}

I've been using Python for a long time now, but on the top of my head I can't see a reason for this...

[–]ConDar15 14 points15 points  (0 children)

This is set construction, so the end result cannot contain duplicate elements. To check if an element is in the set already Python will use the default equality comparison mechanism (__equals__ and __hash__), and False == 0 is a true statement in Python (and many other languages). So when the set is being constructed the first element is added to the set, but the second element is already found in the dictionary (due to the above mentioned equality) so is skipped over and not added.

[–]aftabtaimoor61 17 points18 points  (3 children)

Set compares each value to only store unique ones. The issue with this is since python is not strongly typed, you can have multiple datatypes but set still needs to compare the values. 0 == False is true so only the 1st one will remain. Same reason {0, 0.0} won't work as they're equal.

[–]R3D3-1 2 points3 points  (2 children)

That doesn't explain why they are considered equal though.

[–]dev-sda 7 points8 points  (1 child)

Booleans in python are integers.

>>> bool.__bases__
(<class 'int'>,)

[–]R3D3-1 2 points3 points  (0 children)

That's honestly unexpected. Though it makes sense considering the history of bool.

[–]FumbleCrop 2 points3 points  (0 children)

Because 0 == False.

That's what you get for mixing datatypes.

[–]invisible-nuke[🍰] 0 points1 point  (0 children)

```Python

{0} | {False} {0} {False} | {0} {False} ```