Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Check isinstance and issubclass Relations

Learn to correctly determine instance and subclass relationships and handle invalid inputs safely.

Python practice8 minInheritance & PolymorphismBeginnerLast updated April 8, 2026

Problem statement

You'll implement two helper functions that mirror Python's isinstance and issubclass behavior but are safe: they should return True/False and never raise an exception when given invalid argument types. The learning goal is to understand how isinstance and issubclass behave and to practice defensive checks when working with dynamic inputs. Implement: - is_instance(obj, class_or_tuple): Returns True if obj is an instance of class_or_tuple (like isinstance). If class_or_tuple is not a class or a tuple of classes, return False. - is_subclass(child, parent): Returns True if child is a subclass of parent (like issubclass). If either argument is not a class (or a tuple of classes where appropriate), return False. A small class hierarchy is provided for testing. Your functions must not raise exceptions when passed invalid types; instead return False.

Task

Implement safe wrappers for isinstance and issubclass that return False instead of raising on invalid input types.

Examples

Basic instance check

Input

is_instance(B(), A)

Output

True

B is a subclass of A, so an instance of B is an instance of A.

Input format

Single function call expression. Examples: is_instance(obj, cls) or is_subclass(child, parent).

Output format

Boolean result (True or False). The test harness compares str(return_value) to the expected string.

Constraints

Do not raise exceptions for invalid inputs; return False instead. Do not import external libraries.

Samples

Sample 1

Input

is_subclass(C, A)

Output

True

C inherits from B which inherits from A.