Tips for those who are wondering how to use is and == in Python

is should be used in object comparison

In Python, you might write ʻif x is y or ʻif x == y. At that time, you may get lost with ʻisand==. Therefore, the differences between ʻis and==are summarized below.

--ʻIs compares objects (whether they are the same object) --==` compares values (whether they are the same value)

Use case (when using is)

x = <object A> Is defined, if x is <object A>: This is the case when you want to write a conditional expression.

Of course, for True / False comparisons x = True Define, if x is True: Is not a mistake, but in this case pythonic if x: It is good to write.

→ The reason why x is True is okay is that the bool class True / False is designed so that there is always only one.

The values False and True will be singletons, like None.

Quote source PEP285

Use case (when using ==)

x = 1234 Is defined, if x == 1234: This is the case when you want to write a conditional expression.

Summary

Don't confuse is with ==, and use them properly.

footnote

Footnote 1 Contents presented in the comment section by @ 7of9

https://docs.python.org/3.6/library/functions.html#id

id(object) ... CPython implementation detail: This is the address of the object in memory.

Is the basis of the document.

Footnote 2 Contents presented in the comment section by @shiracamus

I checked the source of CPython. "is" was replaced with Is, Is was replaced with PyCmp_IS, and PyCmp_IS compared the addresses as follows:

Python/ceval.c


static PyObject *
cmp_outcome(int op, register PyObject *v, register PyObject *w)
{
    int res = 0;
    switch (op) {
    case PyCmp_IS:
        res = (v == w);
        break;
    case PyCmp_IS_NOT:
        res = (v != w);
        break;

In the end, we are doing address comparison.

Recommended Posts

Tips for those who are wondering how to use is and == in Python
How to use is and == in Python
[For beginners] How to use say command in python!
Things to keep in mind when using Python for those who use MATLAB
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
For those who are having trouble drawing graphs in python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Tips for coding short and easy to read in Python
Comparison of how to use higher-order functions in Python 2 and 3
For those who are in trouble because NFC is read infinitely when reading NFC with Python
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
How to use functions in separate files Perl and Python versions
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
[Python] Organizing how to use for statements
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
How to use regular expressions in Python
A memo for those who use Python in Visual Studio (me)
How to generate permutations in Python and C ++
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
How to plot autocorrelation and partial autocorrelation in python
OpenGoddard How to use 2-python library for nonlinear optimal control and orbit generation
How to use OpenGoddard 3-python library for nonlinear optimal control and orbit generation
How to use OpenGoddard 4-python library for nonlinear optimal control and orbit generation
For those who want to learn Excel VBA and get started with Python
How to use Service Account OAuth and API with Google API Client for python
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
How to use OpenGoddard 1-python library for nonlinear optimal control and orbit generation
[Python] [Django] How to use ChoiceField and how to add options
How to run python in virtual space (for MacOS)
[Python] How to sort dict in list and instance in list
I tried to summarize how to use pandas in python
For those who want to write Python with vim
How to use the model learned in Lobe in Python
How to use Decorator in Django and how to make it
Python # How to check type and type for super beginners
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
How to use pip, a package management system that is indispensable for using Python
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
For those who are new to programming but have decided to analyze data with Python
Notes on how to use StatsModels that can use linear regression and GLM in python
How to develop in Python
~ Tips for beginners to Python ③ ~
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)