4 ms·
No you misread it, equality for python strings works exactly the way you expect. the id() value (which is basically the memory address) might not change if you
by ltbarcly3 1y ago
No you misread it, equality for python strings works exactly the way you expect. the id() value (which is basically the memory address) might not change if you concatenate strings, but literally nobody ever uses id for anything ever. You would use hash() which does change appropriately.
edit: 'wtf!' is always equal to itself. Maybe you are thinking of `is`, which just compares the value of id(). `is` can't be used for comparisons, saying it's a WTF is just being unfamiliar with basics. `is` is not used for equality comparisons. This is how more or less every language works, comparing pointers is not the same as comparing strings in C/rust/etc. Comparing object identity is not the same as testing equality in Java.
- andy_ppp 1y agoI think honestly it is you who misread my comment... I was saying the id() issue isn't a real problem, the weirdness around "wtf!" not being equal to itself under some circumstances is very confusing. Seems Google Colab isn't loading here.
- ziml77 1y agoBut "is" doesn't check equality. It checks reference equality. Maybe the real wtf here is they made that check a temptingly short infix operator rather than a function named reference_equals().
- zahlman 1y ago"wtf!" is consistently equal to itself in Python. The fact that you could have separate string objects both representing that text, is entirely orthogonal to that.
- Jtsummers 1y ago`is` is more like doing this in C: char* s1 = "wtf!"; char* s2 = "wtf!"; if (s1 == s2) { ... } They may or may not be stored in the same place depending on your compiler (clang and gcc yes, msvc no, at least with just a quick default build). If you actually want to do an equality check, you need to use `==` (in Python) which would be like using `strcmp` in C. > "wtf!" not being equal to itself It is equal to itself, but multiple variables referencing that string may not hold references to the same (literal, in memory) string.