type() and isinstance()

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.
"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."
def add_integer(a, b=98):
"""Adds two integers"""
if not isinstance(a, (int, float)):
raise TypeError('a must be an integer')
if not isinstance(b, (int, float)):
raise TypeError('b must be an integer')
if isinstance(a, float):
a = int(a)
if isinstance(b, float):
b = int(b)
return a + b
When you run the example below, you get 100
>>> add_integer(True, 99)
100
>>>
In Python, the bool type is a subclass of int. Therefore, when you pass True as an argument to your add_integer function, it is considered as an instance of int. As a result, it is not raising a TypeError as you would expect.
To address this and disallow boolean values, you can modify the code to explicitly check for the bool type. Here's an updated version of your code:
def add_integer(a, b=98):
"""Adds two integers"""
if isinstance(a, bool) or isinstance(b, bool):
raise TypeError('a and b must be integers or floats')
if not isinstance(a, (int, float)):
raise TypeError('a must be an integer or float')
if not isinstance(b, (int, float)):
raise TypeError('b must be an integer or float')
if isinstance(a, float):
a = int(a)
if isinstance(b, float):
b = int(b)
return a + b
OR YOU DO THIS INSTEAD
def add_integer(a, b=98):
"""Adds two integers"""
if type(a) is not int and type(a) is not float:
raise TypeError('a must be an integer')
if type(b) is not int and type(b) is not float:
raise TypeError('b must be an integer')
if isinstance(a, float):
a = int(a)
if isinstance(b, float):
b = int(b)
return a + b
Output:
>>> add_integer(True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/alx-higher_level_programming/0x07-python-test_driven_development/0-add_integer.py", line 9, in add_integer
raise TypeError('a must be an integer')
TypeError: a must be an integer
In the original code that used isinstance() for type checking:
The issue is that the bool type is a subclass of int. Therefore, when you pass True as an argument, it is considered as an instance of int and the isinstance(a, (int, float)) check will pass. This is because True is equivalent to 1, which is an int.
On the other hand, when you use type() for type checking:
The type(a) is not int and type(b) is not int checks explicitly check for int types and do not consider bool as a valid type. Therefore, when you pass True as an argument, the check type(a) is not int fails and raises the expected TypeError with the message 'a must be an integer'.
In summary, the use of type() for type checking in your code correctly disallows boolean values, including True, as it explicitly checks for int types. The previous use of isinstance() in my suggestions did not work as expected because bool is considered a subclass of int, leading to a different behavior.
type(True) = bool
type(1) = int



