Skip to main content

python的interface模式

Project description

python的interface模式

它是什么, 有什么用

zinterface是一个帮助python实现接口模式的模块, interface模式能让多人合作开发变得轻松, 能解决代码依赖, 让程序逻辑更加清晰

python有了本身就是鸭子类型的, 为什么还要多此一举

在python中, 一个类可以当成另一个类型来使用, 前提是这个类实现了该类型的方法, 但是在IDE中它不会显示的告诉你这个类是否实现了该类型的所有方法, zinterface能帮助你强行要求某个类必须实现某个类型的所有方法, 能在代码预编译时就会报告某些方法未被实现

如何安装它

pip install zinterface

主页

github.com/zlyuancn/zinterface

导入zinterface

from zinterface import InterfaceBase, InterfaceCheck

创建接口类

@InterfaceBase
class IAA():
    def test(self):  # 需要实现的类型
        pass

创建一个类实现该接口

@InterfaceCheck
class AA(IAA):
    def test(self):
        print('测试')

高级用法实例

from zinterface import InterfaceBase, InterfaceCheck

@InterfaceBase
class IRead():
    def read(self):
        pass

@InterfaceBase
class IWrite():
    def write(self, data):
        pass

@InterfaceBase
class IFile(IRead, IWrite):
    pass

@InterfaceCheck
class MyFileBase(IFile):
    def read(self):
        print('读取')

    def write(self, data):
        print('写入')

a = MyFileBase()
a.read()
a.write(None)

更新日志

发布时间 发布版本 发布说明
19-02-19 0.1.0 发布第一版

本项目仅供所有人学习交流使用, 禁止用于商业用途

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zinterface-0.1.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file zinterface-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: zinterface-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.6.7

File hashes

Hashes for zinterface-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81c89bbf40234224f333d8546a096eadac608fd25f0f881a384360db56c4be51
MD5 b7064a97d4095a19e313c3db6122e2aa
BLAKE2b-256 a7c3f25a3b95f8f7c4ce6e9ff8afbebd5f30469194001bec313ec1d9cb2d9e54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page