Comparing Boolean and Int Using Isinstance

Comparing boolean and int using isinstance

For historic reasons, bool is a subclass of int, so True is an instance of int. (Originally, Python had no bool type, and things that returned truth values returned 1 or 0. When they added bool, True and False had to be drop-in replacements for 1 and 0 as much as possible for backward compatibility, hence the subclassing.)

The correct way to "solve" this depends on exactly what you consider the problem to be.

  • If you want True to stop being an int, well, too bad. That's not going to happen.
  • If you want to detect booleans and handle them differently from other ints, you can do that:

    if isinstance(whatever, bool):
    # special handling
    elif isinstance(whatever, (float, int)):
    # other handling
  • If you want to detect objects whose specific class is exactly float or int, rejecting subclasses, you can do that:

    if type(whatever) in (float, int):
    # Do stuff.
  • If you want to detect all floats and ints, you're already doing that.

Why are booleans considered integers?

It is in the documentation: Numeric Types

There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers.

True and False are numerically equal to 1 and 0 in Python, respectively.

Because bool is a subclass of int, as established above, isinstance returns true, as documented:

isinstance(object, classinfo)

Return True if the object argument is an instance of the classinfo argument, or of a subclass thereof.

Comparing boolean and int using isinstance

For historic reasons, bool is a subclass of int, so True is an instance of int. (Originally, Python had no bool type, and things that returned truth values returned 1 or 0. When they added bool, True and False had to be drop-in replacements for 1 and 0 as much as possible for backward compatibility, hence the subclassing.)

The correct way to "solve" this depends on exactly what you consider the problem to be.

  • If you want True to stop being an int, well, too bad. That's not going to happen.
  • If you want to detect booleans and handle them differently from other ints, you can do that:

    if isinstance(whatever, bool):
    # special handling
    elif isinstance(whatever, (float, int)):
    # other handling
  • If you want to detect objects whose specific class is exactly float or int, rejecting subclasses, you can do that:

    if type(whatever) in (float, int):
    # Do stuff.
  • If you want to detect all floats and ints, you're already doing that.

Python 3 isinstance bool without external validator or type()?

You're on the right track to observe that bool is a subclass of int. The issue is the behavior of isinstance. From the documentation (emphasis added):

isinstance(object, classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If
object is not an object of the given type, the function always returns
false. If classinfo is a tuple of type objects (or recursively, other
such tuples), return true if object is an instance of any of the
types. If classinfo is not a type or tuple of types and such tuples, a
TypeError exception is raised.

Since bool is a subclass of int, any bools passed into your if/elif clauses will not make it past the first if statement, isinstance(<bool>, int) will return True. You need to put the check for bool before the check for int.

What are the differences between type() and isinstance()?

To summarize the contents of other (already good!) answers, isinstance caters for inheritance (an instance of a derived class is an instance of a base class, too), while checking for equality of type does not (it demands identity of types and rejects instances of subtypes, AKA subclasses).

Normally, in Python, you want your code to support inheritance, of course (since inheritance is so handy, it would be bad to stop code using yours from using it!), so isinstance is less bad than checking identity of types because it seamlessly supports inheritance.

It's not that isinstance is good, mind you—it's just less bad than checking equality of types. The normal, Pythonic, preferred solution is almost invariably "duck typing": try using the argument as if it was of a certain desired type, do it in a try/except statement catching all exceptions that could arise if the argument was not in fact of that type (or any other type nicely duck-mimicking it;-), and in the except clause, try something else (using the argument "as if" it was of some other type).

basestring is, however, quite a special case—a builtin type that exists only to let you use isinstance (both str and unicode subclass basestring). Strings are sequences (you could loop over them, index them, slice them, ...), but you generally want to treat them as "scalar" types—it's somewhat incovenient (but a reasonably frequent use case) to treat all kinds of strings (and maybe other scalar types, i.e., ones you can't loop on) one way, all containers (lists, sets, dicts, ...) in another way, and basestring plus isinstance helps you do that—the overall structure of this idiom is something like:

if isinstance(x, basestring)
return treatasscalar(x)
try:
return treatasiter(iter(x))
except TypeError:
return treatasscalar(x)

You could say that basestring is an Abstract Base Class ("ABC")—it offers no concrete functionality to subclasses, but rather exists as a "marker", mainly for use with isinstance. The concept is obviously a growing one in Python, since PEP 3119, which introduces a generalization of it, was accepted and has been implemented starting with Python 2.6 and 3.0.

The PEP makes it clear that, while ABCs can often substitute for duck typing, there is generally no big pressure to do that (see here). ABCs as implemented in recent Python versions do however offer extra goodies: isinstance (and issubclass) can now mean more than just "[an instance of] a derived class" (in particular, any class can be "registered" with an ABC so that it will show as a subclass, and its instances as instances of the ABC); and ABCs can also offer extra convenience to actual subclasses in a very natural way via Template Method design pattern applications (see here and here [[part II]] for more on the TM DP, in general and specifically in Python, independent of ABCs).

For the underlying mechanics of ABC support as offered in Python 2.6, see here; for their 3.1 version, very similar, see here. In both versions, standard library module collections (that's the 3.1 version—for the very similar 2.6 version, see here) offers several useful ABCs.

For the purpose of this answer, the key thing to retain about ABCs (beyond an arguably more natural placement for TM DP functionality, compared to the classic Python alternative of mixin classes such as UserDict.DictMixin) is that they make isinstance (and issubclass) much more attractive and pervasive (in Python 2.6 and going forward) than they used to be (in 2.5 and before), and therefore, by contrast, make checking type equality an even worse practice in recent Python versions than it already used to be.

Bool values in pandas DataFrame converted to ints whilst applying function to multiply numbers

It's because:

>>> isinstance(True, int)
True
>>>

True is actually 1!

And False is actually 0, so you're kinda attempting this approach strangely.

To fix it, use type:

def multi(x):
if type(x) in (float, int)):
return x * 10
return x

Python Bool and int comparison and indexing on list with boolean values

What's going on is that booleans actually are integers. True is 1 and False is 0. Bool is a subtype of int.

>>> isinstance(True, int)
True
>>> issubclass(bool, int)
True

So it's not converting them to integers, it's just using them as integers.

(Bools are ints for historical reasons. Before a bool type existed in Python, people used the integer 0 to mean false and 1 to mean true. So when they added a bool type, they made the boolean values integers in order to maintain backward compatibility with old code that used these integer values. See for instance http://www.peterbe.com/plog/bool-is-int .)

>>> help(True)
Help on bool object:

class bool(int)
| bool(x) -> bool
|
| Returns True when the argument x is true, False otherwise.
| The builtins True and False are the only two instances of the class bool.
| The class bool is a subclass of the class int, and cannot be subclassed.

Is int.class.isInstance(Object) a contradiction?

Not all Class objects represent classes / reference types; there are also Class objects that represent primitive types. This is useful because in using reflection with fields and methods, you often need to specify their type, and it can be a primitive type. So Class is used to represent all such pre-generics types.

However, many of the methods of the Class class do not make sense for primitive types. For example, it is impossible for an object to be instanceof int. Therefore, the analogous .isInstance() method will always return false. Since the parameter of that method is type Object, it is simply impossible from a language point of view for what you pass in there to be of a primitive type.

Sure, in Java 5+ when you pass a primitive to a parameter of type Object, it undergoes autoboxing, but the fact that it underwent autoboxing means that what was passed is actually a reference to an object. Reference types and primitive types are distinct. A parameter is either a reference type or primitive type. Thus you cannot write a method that can take a "reference or primitive".

What you may be asking, in your example, is to detect that the object was autoboxed from a primitive, and compare it to a primitive type. However, it is impossible to detect whether the caller autoboxed it, since autoboxing is a completely caller-side operation that happens before the call.

However, assuming it was autoboxed, you know what type it should have gone to. If you are expecting an int, and it is autoboxed and passed to your method, it should be an instance of Integer. Thus, what you could do is, when clazz represents a primitive type, instead perform the check on its wrapper class. Thus, when it sees that clazz is int.class, substitute it with Integer.class, and then perform the check. Note that this way still doesn't tell whether what was passed as the o parameter was autoboxed.



Related Topics



Leave a reply



Submit