"isinstance" Integer Bug?

Following snippet outputs “No”. Any ideas why? Can anyone replicate the problem.


number = 12345678901

if isinstance(number, int):
    print("Yes")
else:
    print("No")

You can also check this way:

def is_int(val):
    if type(val) == int:
        return "Yes"
    else:
        if val.is_integer():
            return "Yes"
        else:
            return "No"

Try this:

number = 12345678901

if isinstance(number, (int, long)):
    print("Yes")
else:
    print("No")
1 Like

This is about precision of the integer types in Ironpython. In your example, number is defined as an instance of type long (long integer) because it exceeds the system 32-bit precision limit. More info about this here. You can see this with by print type(number). A way to check for either integer types (int and long) would be simply

number = 12345678901
if isinstance(number, (int,long)):
    print("Yes")
else:
    print("No")
1 Like

Thank you Mostapha and David,

I wrongly assumed that all IronPython integers are implemented as long (as in Python).

I wrongly assumed that all IronPython integers are implemented as long (as in Python).

Hi,
A finickity point - It’s not just Ironpython : This was the case in CPython 2.7 as well. It was changed a decade ago with the move to Python 3 :slight_smile:

https://docs.python.org/2/library/stdtypes.html

Make sure you don’t mix C long with Python long. Plain integers (int) in Python are implemented with C long, but are still called integers. Python long has no limit (other than what fits in memory).