gpt4 book ai didi

python - issubclass 的用法

转载 作者:太空狗 更新时间:2023-10-30 02:05:44 26 4
gpt4 key购买 nike

>>> import sys
>>> sys.version_info
(2, 4, 4, 'final', 0)
>>> class C:
... pass
...
>>> issubclass(C, C)
True
>>> issubclass(C, object)
False
>>> class T(object):
... pass
...
>>> issubclass(T, T)
True
>>> issubclass(T, object)
True
>>>

问题1>为什么CC的子类?

问题2>C的基类是什么?

谢谢

//Chris Morgan 的更新(至少对我来说,以下手册根本没有帮助)

>>> help(issubclass)
Help on built-in function issubclass in module __builtin__:

issubclass(...)
issubclass(C, B) -> bool

Return whether class C is a subclass (i.e., a derived class) of class B.
When using a tuple as the second argument issubclass(X, (A, B, ...)),
is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).

最佳答案

看看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.

并检查 C 的基类使用 inspect.getmro(cls)功能。

Return a tuple of class cls’s base classes, including cls, in method resolution order.

   >>> class C(object):
... pass
...
>>> inspect.getmro(C)
(<class '__main__.C'>, <type 'object'>)
>>>

关于python - issubclass 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9238181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com