I've tried this using a regular dictionary by inserting the key / value manually and it evaluates to False, as expected. But when I try it here, it evaluates to True.
Strings in Python, Java etc are a clever language construct which returns an object reference. In the background, memory is allocated and populated with the character bytes, while the object itself refers to some range of that memory. The intention is to re-use the memory if it can, but the trade-off is to make it more difficult to distinguish between two identifiers/variables which contain:
* The same object reference (the purpose of is and is not)
* Different objects which refer to the same underlying range on the string heap
* Different objects with different heap ranges, but the actual character content still matches
Various language API and library API will create strings in different ways, so you almost never know which of the above it is and probably shouldn't rely on any particular behaviour quirk with it since it won't stay constant between languages or even release versions within one language.
For protection against an even bigger mess, read about how Python handles integers...
7 comments
0 u/user9713 [OP] 29 Sep 2019 05:35
I've tried this using a regular dictionary by inserting the key / value manually and it evaluates to False, as expected. But when I try it here, it evaluates to True.
FWIW,
successis a string0 u/ShowMeYourKitties 29 Sep 2019 05:38
Because one is checking if the value of the string is equal to the value of another string.
The
is notis checking if they are the same instance or space in memory.0 u/user9713 [OP] 29 Sep 2019 05:41
Ah, I see. So just use
==instead?0 u/theoldones 29 Sep 2019 06:03
!= means "anything but this"
= means "this"
0 u/Element115 29 Sep 2019 07:39
That's also true of is this and is not this
0 u/SithEmpire 29 Sep 2019 09:01
Adding a general note about this -
Strings in Python, Java etc are a clever language construct which returns an object reference. In the background, memory is allocated and populated with the character bytes, while the object itself refers to some range of that memory. The intention is to re-use the memory if it can, but the trade-off is to make it more difficult to distinguish between two identifiers/variables which contain:
* The same object reference (the purpose of is and is not)
* Different objects which refer to the same underlying range on the string heap
* Different objects with different heap ranges, but the actual character content still matches
Various language API and library API will create strings in different ways, so you almost never know which of the above it is and probably shouldn't rely on any particular behaviour quirk with it since it won't stay constant between languages or even release versions within one language.
For protection against an even bigger mess, read about how Python handles integers...
0 u/user9713 [OP] 29 Sep 2019 13:27
That's pretty interesting, thanks for sharing. And yeah, that is a mess lol.