How to use is and == in Python

I've only been in Python for about a year now, but I've noticed that I couldn't clearly distinguish between the comparison operators ʻisand==`. Described as a commandment to yourself.

1. Organize

ʻIsis Object Identity ==` is Object Equality

Is. As the name implies, Object Identity determines whether they are the same object.

On the other hand, for ==, it is the same implementation as the __eq__ method. For example, it is determined whether or not the character strings match even between different objects. https://docs.python.org/3/reference/datamodel.html#object.eq

a = 'hoge'

print(a.__eq__('hoge'))  # True
  1. Usage
a = None

if a == None:
    print('Not good')

On the other hand, if it complies with PEP8, the following will be alerted. E711 comparison to None should be 'if cond is Nond:'

This says that for singletons like None, identity in Object Identity should be compared. Therefore, it is desirable to write as follows.

a = None

if a is None:
    print('Good')

However, if it is not necessary to clearly indicate that it is None, ↓ is Pythonic. (3. I wrote it in a digression)

a = None

if not a:
    print('Good')

Also, for example, if ʻisis used for character string comparison as shown below, it cannot be evaluated properly. If you want to make a comparison such as strings match, you need to use==`. (Also, in the case of string comparison, you should also be careful about unicode or str)

a = 'hoghoge'

if a is 'hogehoge':
    print('This is not called!')
else:
    print('This is called!')

3. Digression

if a:

The if clause is also often used in Python.

this is,

if a is not None:

Unlike a,

It means that it is different from any of.

If it is better to make an explicit comparison with these,

ʻIf a is not : `

If not,

if a:

OK.

4. Summary

Make sure you understand the usage and write correct, Pythonic code.

Recommended Posts

How to use is and == in Python
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
python: How to use locals () and globals ()
How to use Python zip and enumerate
How to use regular expressions in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
How to generate permutations in Python and C ++
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python Image Library in python3 series
How to use functions in separate files Perl and Python versions
Summary of how to use MNIST in Python
How to use Python argparse
How to use the asterisk (*) in Python. Maybe this is all? ..
Python: How to use pydub
[Python] How to use checkio
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
How to develop in Python
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to plot autocorrelation and partial autocorrelation in python
How to use Python bytes
[For beginners] How to use say command in python!
[Python] [Django] How to use ChoiceField and how to add options
[Python] How to sort dict in list and instance in list
I tried to summarize how to use pandas in python
How to use Decorator in Django and how to make it
[Python] How to do PCA in Python
Python: How to use async with
Difference between == and is in python
Use fabric as is in python (fabric3)
How to install and use Tesseract-OCR
How to use classes in Theano
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use .bash_profile and .bashrc
How to install and use Graphviz
[Python] How to use list 3 Added
How to use OpenPose's Python API
How to use FTP with Python
Python: How to use pydub (playback)
How to use python zip function
How to handle Japanese in Python
[Python] How to use Typetalk API
How to test that Exception is raised in python unittest
How to swap elements in an array in Python, and how to reverse an array.
How to use the __call__ method in a Python class
How to use pyenv and pyenv-virtualenv in your own way
[Python] Summary of how to use split and join functions
How to create and use static / dynamic libraries in C
How to execute external shell scripts and commands in python