【发布时间】:2019-10-09 08:49:51
【问题描述】:
我试图找出两个类是否等效,忽略类型参数。说我有
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
pass
class B(Generic[T], A[T]):
pass
class X:
pass
我希望后面的每一行都是等价的
Generic, Generic[T]
A, A[T], A[str], A[int]
B, B[T], B[str], B[int]
X
is、==、isinstance、type 或 __class__ 均无效。比较 __name__ 对于定义另一个具有相同名称的类的人来说是脆弱的。
对于奖励积分*,我还对另一种测试等效性的方法感兴趣
A, A[T], A[str], A[int], B, B[T], B[str], B[int]
*不是赏金:p
(上下文是我想查找除Generic之外的一个类的所有子类)
【问题讨论】:
标签: python python-3.x generics equality python-typing