Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通用接口/C++模板等价的Python类型提示_Python_Templates_Interface_Typing - Fatal编程技术网

通用接口/C++模板等价的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

我想要一个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 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需要先确认,但你明白了