Skip to content Skip to sidebar Skip to footer

Is Keyword In Python

When I read 'Learning Python', I'm confused about using the is operator. The book tries to explain it as a test for the same memory address (A is B, if True, means A and B are in t

Solution 1:

This is an implementation detail of CPython (the standard Python interpreter), which will reuse the same data in memory for some immutable types such as strings and integers. You can't rely on such behavior, therefore you should always use == to compare such types.

For a more in-depth answer, see https://stackoverflow.com/a/15541556/1544347

Solution 2:

Is compares the reference, "==" is syntactical sugar for the "eq" methode.

So when you are testing with "==" the values of two string must be equal to be true. If you testing with "is" the objects must be referential the same.

Post a Comment for "Is Keyword In Python"