Determine if the type in the first argument is a subclass of second in Python


To determine if the type in the first argument is a subclass of second, use the numpy.issubsctype() method in Python numpy. The 1st and the 2nd argument are datatypes.

Steps

At first, import the required library −

import numpy as np

Using the issubsctype() method in Numpy. Checking whether the first argument is a subclass of the second argument −

print("Result...",np.issubsctype(np.float16, np.float32))
print("Result...",np.issubsctype(np.int32, np.signedinteger))
print("Result...",np.issubsctype('i4', np.signedinteger))
print("Result...",np.issubsctype('S8', str))
print("Result...",np.issubsctype(np.array([45, 89]), int))
print("Result...",np.issubsctype(np.array([5., 25., 40.]), float))

Example

import numpy as np

# To determine if the type in the first argument is a subclass of second, use the numpy.issubsctype() method in Python numpy
# The 1st and the 2nd argument are datatypes

print("Using the issubsctype() method in Numpy\n")

# Checking whether the first argument is a subclass of the second argument
print("Result...",np.issubsctype(np.float16, np.float32))
print("Result...",np.issubsctype(np.int32, np.signedinteger))
print("Result...",np.issubsctype('i4', np.signedinteger))
print("Result...",np.issubsctype('S8', str))
print("Result...",np.issubsctype(np.array([45, 89]), int))
print("Result...",np.issubsctype(np.array([5., 25., 40.]), float))

Output

Using the issubsctype() method in Numpy

Result... False
Result... True
Result... True
Result... False
Result... True
Result... True

Updated on: 24-Feb-2022

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements