python中接口的一种新方法

strict-interfaces的Python项目详细描述


python严格接口

安装

pip install strict-interfaces

设计目标

  • 尽可能严格
  • 导入时间失败
  • 不要弄乱object和/或type继承
  • 集成到Cpyhon Core中的可能性
  • 能够使用“开箱即用”而不必考虑译员的支持

功能

  • 类定义上的特殊关键字implements
  • 多接口实现
  • 隐式接口实现
  • 限制重载的接口继承
  • 特殊的isimplementation功能类似于issubclass
  • 部分issubclass支持(见下文)
  • 仅限于创建接口实例
  • 限制同时从objectinterface继承

用法

显式实现

classTestInterface(interfaces.interface):defmethod(self,arg:typeT1)->typeT2:passclassTestClass(interfaces.object,implements=[TestInterface]):defmethod(self,arg:typeT1)->typeT2:pass

未实现时引发

classTestInterface(interfaces.interface):defmethod(self,arg):passclassTestClass(interfaces.object,implements=[TestInterface]):pass

隐式实现和运行时检查

classTestInterfaceA(interfaces.interface):defmethod_a(arg:typeT1)->typeT1:passclassTestInterfaceB(interfaces.interface):defmethod_b(arg:typeT2)->typeT2:passclassTestClass:defmethod_a(arg:typeT1)->typeT1:passdefmethod_b(arg:typeT2)->typeT2:passassertinterfaces.isimplementation(TestClass,(TestInterfaceA,TestInterfaceB))

isimplementation检查是否实现了所有接口

classTestInterfaceA(interfaces.interface):defmethod_a(arg:typeT1)->typeT1:passclassTestInterfaceB(interfaces.interface):defmethod_b(arg:typeT2)->typeT2:passclassTestClass:defmethod_a(arg:typeT1)->typeT1:pass# NOTE: In this case `isimplementation` behaves different than `issubclass`assertnotinterfaces.isimplementation(TestClass,(TestInterfaceA,TestInterfaceB))assertissubclass(TestClass,(TestInterfaceA,TestInterfaceB))

贡献

拉式请求、功能请求和错误报告总是受欢迎的!

github.com/lig/python-interfaces

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
junit cucumber为什么会找到“runTest.java”来运行测试?   在Eclipse中找不到java KeyPairGenerator   java NotSerializableException即使在实现Serializable之后   noclassdeffounderror(java字符串连接)为什么会出现这种异常?   java Guice:将接口绑定到由动态代理创建的实例   使用Spring数据neo4j创建空间索引时发生java错误   java对于需要在50多个excel文件上运行并且每个文件平均包含25k行的项目,最佳的方法是什么   javaNIO中的java缓冲区写入/发送消息问题   如何在Java/eclipse中添加不调用super()的警告   JavaSpring:mvcUrl映射错误的id   java应该在getInstance或构造函数中使用Init方法吗?   安卓中的java空指针异常错误   java Jsoup不能完全获取原始html代码