LoginSignup
2
2

More than 5 years have passed since last update.

python2での整数判定

Posted at

python2には整数を表す型にint(整数)とlong(長整数)の2つが存在しています。
このlongはintと共通のサブクラスを持たないので注意が必要です。
例えば変数が整数値であるか判定するのに以下のようにする必要があります。

if isinstance(value, (int, long)): # isinstance(value, int) ではダメ
    print('OK')

また、bool型はintをサブクラスに持つので、boolを取り除くのに以下のようにしてはじきます。

if isinstance(value, (int, long)) and not (isinstance(value, bool)):
    print('OK')

通常pythonを使う上でlongはあまり使われないのであまり問題ないのですが、dbから値を取ってくるときにormがdbの型に合わせてlongを使うことがあります。
python3系だとlongは廃止されintに集約されています。
boolはpython3でもintのサブクラスのままです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2