通用接口/C++模板等价的Python类型提示
我想要一个python中的通用类型提示接口。 我想大致达到以下目标,但我一直在寻找解决方案:通用接口/C++模板等价的Python类型提示,python,templates,interface,typing,Python,Templates,Interface,Typing,我想要一个python中的通用类型提示接口。 我想大致达到以下目标,但我一直在寻找解决方案: T = TypeVar('T') S = TypeVar('S') class GenericInterface(ABC): @abstractmethod def get(self, number: T)->S: pass def get_example()->GenericInterface[int, str]: class Exampl
T = TypeVar('T')
S = TypeVar('S')
class GenericInterface(ABC):
@abstractmethod
def get(self, number: T)->S:
pass
def get_example()->GenericInterface[int, str]:
class Example(GenericInterface[int, str]):
def get(self, number: int)->str:
return str(number)
return Example()
因此,在上面的示例中,GenericInterface[int,str]应该描述以下类型化接口:
class MyInterface(ABC):
@abstractmethod
def get(self, number: int)->str:
pass
我想这就是你想要的:
类GenericInterfaceGeneric[T,S],ABC:
@抽象方法
def getself,编号:T->S:
通过
诚然,我不确定是通用还是ABC需要先确认,但你明白了