7 comments

0

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, success is a string

0

Because one is checking if the value of the string is equal to the value of another string.

The is not is checking if they are the same instance or space in memory.

0

Ah, I see. So just use == instead?

0

!= means "anything but this"

= means "this"

0

That's also true of is this and is not this

0

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

That's pretty interesting, thanks for sharing. And yeah, that is a mess lol.