Search code examples
pythonisinstance

Why is this Isinstance function not working?


This does not print anything for some reason.


main = 60

x = isinstance(main, int)

if x == ("True"):  
   print("True") 

elif x == ("False"):  
   print("False")


Solution

  • The boolean value which is returned by the isinstance() function is not a string, but (as you may been able to guess) a boolean.

    To rewrite the code provided:

    main = 60
    x = isinstance(main, int)
    if x is True:
        print("True")
    elif x is False:
        print("False")
    

    However, since booleans can be conditions themselves, and isinstance() returns a boolean, I would recommend this approach:

    main = 60
    if isinstance(main, int):
        print("True")
    else:
        print("False")
    

    Alternatively, even simpler:

    main = 60
    print(isinstance(main, int))