相关疑难解决方法(0)

python输入模块:Mixin

是否有任何课程typing表现得像混合?

例如

from typing import Union
class A:
  pass

class B:
  pass

class C:
  pass

class D(A, B, C):
  pass

# current: ab is A or B, but not both
def f(ab: Union[A, B]):
    pass

# ideal: ab is A and B
def f(ab: Mixin[A, B]):
    pass

f(D())
Run Code Online (Sandbox Code Playgroud)

请注意如何DA和的实例B,但也C.这对于f(因为f不需要C)限制太多,因此,参数ab不一定是类型D而是Mixin[A, B]

如果typing模块没有提供任何选项,有没有比创建自己更优雅的东西class AB(A, B)

python multiple-inheritance typechecking

16
推荐指数
1
解决办法
361
查看次数