3 ms·
Most OO languages discriminate between identity and equality (e.g. == vs .equals() in Java, or id() vs == in Python). The relational model nails it even more ex
by nine_k 8y ago
Most OO languages discriminate between identity and equality (e.g. == vs .equals() in Java, or id() vs == in Python). The relational model nails it even more explicitly.
- teddyh 8y agoYou probably mean “‘is’ vs ‘==’ in Python” – nobody should use id() for identity comparisons in Python. For instance, two Python objects may, if they have non-overlapping lifetimes, have the same id() value.
- recursive 8y agoIf they don't have overlapping lifetimes, there would seem to be very little risk of accidentally considering them to be equal, because you could never reference them in the same expression.
- saghm 8y agoNot having heard of the `id` function in Python before, it still sounds like you can make mistakes with this; what if you saved the result of the `id` call of the first value into a variable and then compared it to the result of calling `id` on other values?
- recursive 8y agoI'm sure this could happen somehow, but it's difficult to think of a concrete example of where it would matter.
- fulafel 8y agoMake wrong lifetime assumptions and you get data corruption. I can imagine someone using it eg as an identifier in a web form, and passing it back to the app to lookup in some keyed data structure where they have used id as the keyfn - and getting back the incorrect object because the id has been recycled. Then it's saved to the db and you get data corruption.
- a1369209993 8y agoTrue in practice, but you can reference them in the same expression if one lifetime ends at the expression and the other is created there. Eg: p = (id([]) == id([])) : mklist 0 # alloc -> 0x1234 : call id # free(0x1234) : mklist 0 # alloc -> 0x1234 (free list is a stack) : call id # free(0x1234) : cmp == # true
- nickbauman 8y agoSure. But the problem stems not from identity and equality in OO. It stems from the coupling of state and identity, which has the side effect of muddying the waters between identity and equality too. To whit this discussion.