Detailed introduction to Python's built-in issubclass function

高洛峰
Release: 2017-03-21 09:28:17
Original
1943 people have browsed it

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
Copy after login

 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
Copy after login

 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
Copy after login

 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 "<pyshell#14>", line 1, in <module>
    issubclass(bool,[str,int])
TypeError: issubclass() arg 2 must be a class or tuple of classes
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template