Home  >  Article  >  Backend Development  >  Detailed introduction to Python’s built-in issubclass function

Detailed introduction to Python’s built-in issubclass function

高洛峰
高洛峰Original
2017-03-21 09:28:171887browse

English documentation:

issubclass(class, classinfo)

Return true if class is a subclass (direct, indirect or virtual) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised.

Description:

 1. FunctionThe function is used to determine whether one type object is a subclass of another type object, class The parameter indicates the type object that needs to be checked, and the calssinfo parameter indicates the type object that needs to be compared.

 2. If the class parameter is an instance of a classinfo type object (or a direct, indirect, or virtual subclass of a classinfo class object), return True.

>>> issubclass(bool,int)
True
>>> issubclass(bool,(str))
False

>>> class A:
    pass
>>> class B(A):
    pass
>>> issubclass(B,A)
True

 3. Any class is a subclass of its own class, that is, when class and calssinfo are passed in the same type, True is returned.

>>> class A:
    pass
>>> issubclass(A,A)
True

 4. If the classinfo type object is a tuple composed of multiple type objects, and if the class type object is a subclass of any type object of the tuple, True is returned, otherwise False is returned.

>>> issubclass(bool,int)
True>>> issubclass(bool,str)
False>>> issubclass(bool,(str,int))
True

 5. If the classinfo type object is not a type object or a tuple composed of multiple type objects, an error (TypeError) will be reported.

>>> issubclass(bool,[str,int])
Traceback (most recent call last):
  File "ffae55b2fb00f5ed70ba1d9647e9051b", line 1, in 4225fa317875f3e92281a7b1a5733569
    issubclass(bool,[str,int])
TypeError: issubclass() arg 2 must be a class or tuple of classes

The above is the detailed content of Detailed introduction to Python’s built-in issubclass function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn