Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

How to Check Whether a Python Variable Is an Integer (and Avoid the `bool` Trap)

Use an isinstance check with an explicit bool exclusion for integer subclasses, or type(value) is int when only the exact built-in type should pass.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To accept integers and their subclasses but reject True and False, use isinstance(value, int) and not isinstance(value, bool). If you want only the exact built-in int type, use type(value) is int. The right check depends on whether integer subclasses should count.

How do I check if a variable is an integer in Python?

Choose the check that matches your rule:

  • Accept int subclasses but exclude booleans: isinstance(value, int) and not isinstance(value, bool).
  • Accept only the exact built-in int type: type(value) is int.

For example:

value = 42

is_integer = isinstance(value, int) and not isinstance(value, bool)
is_exact_builtin_int = type(value) is int

isinstance(value, int) accepts instances of int and its subclasses. Since bool is itself a subclass of int, that test alone also accepts True and False. The first expression excludes booleans while still allowing other int subclasses. The second excludes booleans and all other subclasses, so use it only when exact type identity is required. See Python’s boolean type documentation and documentation for isinstance.

Why does isinstance(True, int) return True?

Because Python defines bool as a subclass of int. Consequently, True and False behave like the integers 1 and 0 in many numeric contexts, and both satisfy isinstance(value, int). That inheritance is part of Python’s type semantics, not an error in isinstance.

PEP 285, “Adding a bool type,” explains the compatibility rationale: “Because bool inherits from int, True+1 is valid and equals 2, and so on.” If your application means an integer but not a truth value, add the explicit boolean exclusion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should I use operator.index() instead?

Use operator.index(value) when your code needs to accept objects that implement Python’s lossless integer-index protocol, rather than test whether the original object has a particular type. For example, Python uses this protocol for operations such as indexing and the built-in bin, hex, and oct functions. The data model describes __index__ as the method for this lossless integer conversion: Python data model: object.__index__.

Call the function and handle TypeError if the object does not support the protocol:

import operator

try:
    index_value = operator.index(value)
except TypeError:
    # value does not support the integer-index protocol
    pass
else:
    # index_value is the resulting integer
    pass

This is a conversion attempt, not a test that the original value is exactly a built-in int. It answers a different question from either isinstance or type(value) is int.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why not use int(value) as the check?

int(value) converts a value when possible; it does not tell you whether the original object was an integer. A successful conversion therefore does not establish that the input was an int instance. Decide whether you need subclass membership, exact type identity, or integer-index protocol support, then use the corresponding check.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.