When Python `IS` is not what it is

This is a quick tip on using is, when to use, and when not to use it and what you should really consider when using it.

This is an issue that can affect your results, the effect can be small or big depending on the outcome of using the is operator.

In this small tutorial I will show you how is is not really what is

Comparing Integers and Strings

In this section we will be comparing integers and string and test if they are equal to each others

    x = 1
    y = 1

    print('x is y', bool(x is y))

    ('x is y', True)

That's great ! Now we will try to replicate this to another pair ov values for x and y

    x = 1000
    y = 1000

    print('x is y', bool(x is y))

    ('x is y', False)

As you can see, the comparison is returning False here ! Let's test with some float numbers instead of integers

    x = 0.005
    y = 0.005

    print('x is y', bool(x is y))

    ('x is y', False)

Strange ? What is this type of comparison was added in a program that compares pvalues ? or selecting a type of action based on a threshold ?

Ok let's keep going with Strings this time

    x = "I am Superman"
    y = "I am Ironman"

    print('x is y', bool(x is y))

    ('x is y', False)

This sounds correct

    x = "I am Superman"
    y = "I am Superman"

    print('x is y', bool(x is y))

    ('x is y', False)

Again this is returning False and this may be starting to make you think .. why on earth am using Python ?

No worries ! here is a simple explanation and a small trick on when to use (or not) the is operator

Think of your variables as objects

When you declare two variable with the same value, either strings, integers or floats you actually are creating objects that are not related to each other.

a = "Hulk" --> Object1  
b = "Hulk" --> Object2  

When you compare a is b it is like you are asking if Object1 is Object2, which of course will be returning False

Now let's correct this a bit, I will be revisiting all the examples above but making sure I am using the same Object this time, and see how the result will be

    # Integers

    x = 1000
    #y = 1000
    y = x

    print('x is y', bool(x is y))

    # Floats

    x = 0.005
    #y = 0.005
    y = x

    print('x is y', bool(x is y))

    # Strings

    x = "I am Superman"
    #y = "I am Ironman"

    y = x

    print('x is y', bool(x is y))

    ('x is y', True)
    ('x is y', True)
    ('x is y', True)

Now that we are comparing same objects the results start to make more sense.

But can we still use different objects and still manage to get the right comparison ?

The answer is yes, instead of is use == , let's revisit the previous examples

    # Integers

    x = 1000
    y = 1000

    print('x is y', bool(x == y))

    # Floats

    x = 0.005
    y = 0.005

    print('x is y', bool(x == y))

    # Strings

    x = "I am Superman"
    y= "I am Superman"

    print('x is y', bool(x == y))

    ('x is y', True)
    ('x is y', True)
    ('x is y', True)

Ok : now we are comparing values of objects which returns the correct answer

That's it for today, I hope this will make you think that python is not as straightforward than it seems to be, in comparison to other languages that is correct, but you still need to pay attention to these small things that may affect deeply your results and your data description

Why is it working for small numbers ?

== is a value comparison, is is an object identity (memory location) comparison. You will often see that comparisons like x = 0 will give the intended result because small values are usually cached in Python, but you always want to be using == instead of is when checking equality because this behavior cannot be relied upon.

More .. I think the current implementation is setting the cache range from -5 to 256

    # upper range

    x = 256
    y = 256
    print('x is y', bool(x is y))

    x = 257
    y = 257
    print('x is y', bool(x is y))

    #lower range

    x = -5
    y = -5
    print('x is y', bool(x is y))

    x = -6
    y = -6
    print('x is y', bool(x is y))

    ('x is y', True)
    ('x is y', False)
    ('x is y', True)
    ('x is y', False)



comments powered by Disqus