Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python '!=' Is Not 'is not': Comparing Objects in Python (Summary)

In this course, you learned that == and != compare the value of two objects, whereas the Python is and is not operators compare whether two variables refer to the same object in memory. If you keep this distinction in mind, then you should be able to prevent unexpected behavior in your code.

If you want to read more about the wonderful world of object interning and the Python is operator, then check out Why you should almost never use “is” in Python. You could also have a look at how you can use sys.intern() to optimize memory usage and comparison times for strings, although the chances are that Python already automatically handles this for you behind the scenes.

Now that you’ve learned what the equality and identity operators do under the hood, you can try writing your own __eq__() class methods, which define how instances of this class are compared when using the == operator. Go and apply your newfound knowledge of these Python comparison operators!


Sample Code (.zip)

2.5 KB


Course Slides (.pdf)

260.6 KB

Kevin Dienst on April 1, 2020

I learned about interning! I had encountered this concept before but wasn’t aware of the lower and upper bounds (-5 to 256), nor was I aware of using the intern() method itself. Pretty cool!

Liam Pulsifer RP Team on April 3, 2020

Great to hear, Kevin! Glad you got a chance to learn something new :)

bday29 on April 9, 2020

I was following along in pycharm and the variables, “a” and “b” when set to, “This is a string”, are equal when using the is comparison operator. I am guessing that pycharm intern’s string automagically for us or something.

bday29 on April 9, 2020

I thought these lessons were very helpful, thank you.

Liam Pulsifer RP Team on April 9, 2020

Interesting @bday29. I suppose that could be possible – pycharm does a lot of cool optimizations under the hood. Glad you found the lessons helpful otherwise!

d0ctajones on April 22, 2020

Thanks for the course, Liam! Very helpful in explaining the differences. Had never heard of “intern” before so I did a little research. Based on this excellent Medium article, string interning follows a few simple rules:

  1. The string must be a compile-time constant
  2. The string must be not be subject to constant folding or no longer than 20 characters
  3. The string consists exclusively of ASCII letters, digits, or underscores

When I was following along with experimenting with the interning example #3 is the one that tricked me. I was using short strings with no spaces and it seemed like they were getting automatically interned. This was true! Spaces in the string disqualify it from being automatically interned. is statements worked like a charm on the short, space-less strings.

Hope this helps someone else who’s learning and is too lazy to type longer strings. Thanks again.

Liam Pulsifer RP Team on April 30, 2020

Thanks for that super informative comment @d0ctajones! I wasn’t even aware of all of the specifics of the third rule, so you’ve taught me something today :)

paolotagliente on July 10, 2020

Really liked the way you explained this stuff, i liked also the approach of going over a few times some concepts and at good pace.....i like the colour of the screen where the commands popped out nicely....good work....i am pretty new to python and i got to say that i really like your way to cover and teach stuff…thanks!

Liam Pulsifer RP Team on July 11, 2020

Really glad I could be of help @paolotagliente! Stay tuned for some new tutorials I’ll have out in the coming weeks :)

andersstenborgmartin on July 16, 2020

Good course. Looking forward to new tutorials from you!

Zarata on Aug. 11, 2020

Ditto the comments above, Liam. As you’ve implied, and just to mention to other newbies like me: “it’s complicated”. E.g. the overwrite of __eq__ has implications on the hash-a-bility of an object such as the example user-defined SillyString. Other RP tutorials point to and The first states “Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().” Actually, it’s id()>>4. However, hash(SillyString('a')) returns for me TypeError: unhashable type: 'SillyString'. The simple overwrite of __eq__ in SillyString breaks an __eq__, __hash__ contract honored in the default. There be dragons here!

Ghani on Oct. 15, 2020

Very good course; thanks!

Become a Member to join the conversation.