Filter out boolean as non-integer?
<p>I have always wondered about the following code snippet:    </p>

<pre><code>import math
def func(n):
    if not isinstance(n, int):
        raise TypeError('input is not an integer')
    return math.factorial(n)

print(func(True))
print(func(False))
</code></pre>

<p>I'm always surprised at the result because <code>True</code> and <code>False</code> actually work and are interpreted as the integers <code>1</code> and <code>0</code>. Therefore the factorial function produces the expected results <code>1</code> and <code>1</code> when using <code>True</code> and <code>False</code>. The behavior of those booleans is clearly <a href="https://docs.python.org/3.5/library/stdtypes.html#numeric-types-int-float-complex" rel="nofollow">described in the python manual</a> and for the most part I can live with the fact that a boolean is a subtype of integer.</p>

<p>However, I was wondering: is there any clever way to wash away something like <code>True</code> as an actual parameter for the factorial function (or any other context which requires integers) in a way that it'll throw some kind of exception which the programmer can handle?</p>

<p>Type <code>bool</code> is a <em>subtype</em> of <code>int</code> and <a href="https://docs.python.org/2/library/functions.html#isinstance" rel="nofollow"><code>isinstance</code></a> can walk through inheritance to pass <code>True</code> as an <code>int</code> type. </p>

<p>Use the more stricter <code>type</code>:</p>

<pre><code>if type(n) is not int:
    raise TypeError('input is not an integer')
</code></pre>

python integer boolean