3 ms·
In Python, (a == b) is always value equality, and (a is b) is always reference equality. The only unfortunate aspect is that == is defined for all types - IMO i
by int_19h 4y ago
In Python, (a == b) is always value equality, and (a is b) is always reference equality. The only unfortunate aspect is that == is defined for all types - IMO it really should default to throw, and only classes for which value equality makes sense should define it.
- deleted 4y ago[deleted]
- nine_k 4y agoDefaulting to False is the next best thing. Evil starts when objects of different types are considered value-equal under some circumstances. Python does not have this, with the benign exception of promoting ints to floats, so that 1 == 1.0.
- int_19h 4y agoIt doesn't quite default to False, since every object is still equal to itself. The consequence is that people use == for identity equality sometimes - often not fully intentionally, e.g. by using objects as dict keys directly without using id(). But, conversely, a design that doesn't allow == for everything means that value-equality and reference-equality dicts would have to be different, as well.