Skip to main content

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 发布第一版

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

Metadata

Release files for zinterface 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for zinterface 0.1.0
File Interpreter ABI Platform
zinterface-0.1.0-py3-none-any.whl Python 3 none any Details

Release files / zinterface-0.1.0-py3-none-any.whl

Download URL zinterface-0.1.0-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81c89bbf40234224f333d8546a096eadac608fd25f0f881a384360db56c4be51
BLAKE2b-256 checksum
How to use checksums
a7c3f25a3b95f8f7c4ce6e9ff8afbebd5f30469194001bec313ec1d9cb2d9e54
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for zinterface 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page