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>This piece of code seems to distinguish between boolean and integer parameters in functions. What am I missing?</p>

<pre><code>import math
def func(n):
    if type(n) == type(True):
        print "This is a boolean parameter"
    else:
        print "This is not a boolean parameter"
    if not isinstance(n, int):
        raise TypeError('input is not an integer')
    return math.factorial(n)

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

python integer boolean