python不支持接口,不过我们可以利用第三方库 zope.inteface 实现接口,下面是示例。
from zope.interface import Interface from zope.interface import implementer import socket class IHostNameResolver(Interface): def getHostByName(name, timeout = (1, 3, 11, 45)): # 可以不用self """ Resolve the domain name C{name} into an IP address. """ """ 类装饰器等价于:implementer(IHostNameResolver)(HostNameResolver) """ @implementer(IHostNameResolver) class HostNameResolver: def getHostByName(self, name, timeout = (1, 3, 11, 45)): try: address = socket.gethostbyname(name) except socket.error: return 'none' else: return address resolver = HostNameResolver() print resolver.getHostByName('localhost') # 判断HostNameResolver是否实现了接口IHostNameResolver print IHostNameResolver.implementedBy(HostNameResolver) # True, 调用 SpecificationBasePy.implementedBy()
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索class
接口
zope.interface、zope.interface 安装、zope.interface是什么、zope.interface 下载、zope.interface linux,以便于您获取更多的相关知识。
时间: 2024-10-03 14:20:02