Skip to main content

Mutable Object Declaration - 支持声明与实现分离的类定义库

Project description

mutobj

Python 3.11+ License: MIT

mutobj (Mutable Object Declaration) - 支持声明与实现分离的 Python 类定义库。

特性

  • 声明与实现分离 - 类似 C/C++ 头文件模式
  • Extension 机制 - 为类添加私有状态而不污染接口
  • IDE 友好 - 完整支持 mypy/pyright 和 IDE 跳转
  • 继承支持 - 完整的类继承和方法覆盖
  • 零依赖 - 纯 Python 实现,无外部依赖

安装

pip install mutobj

快速开始

声明文件 (user.py)

import mutobj

class User(mutobj.Declaration):
    name: str
    age: int

    def greet(self) -> str:
        """返回问候语"""
        ...

实现文件 (user_impl.py)

import mutobj
from .user import User

@mutobj.impl(User.greet)
def greet(self: User) -> str:
    return f"Hello, I'm {self.name}!"

使用

from user import User
import user_impl  # 导入以注册实现

user = User(name="Alice", age=25)
print(user.greet())  # "Hello, I'm Alice!"

Extension 机制

为类添加私有状态:

class UserExt(mutobj.Extension[User]):
    _login_count: int = 0

    def __extension_init__(self):
        self._login_count = 0

@mutobj.impl(User.greet)
def greet(self: User) -> str:
    ext = UserExt.of(self)
    ext._login_count += 1
    return f"Hello, {self.name}! (login #{ext._login_count})"

支持的功能

功能 状态
属性声明
方法声明与实现
Property
classmethod
staticmethod
继承
Extension
类型检查

文档

开发

# 安装开发依赖
pip install -e ".[dev]"

# 运行测试
pytest

# 类型检查
mypy src/mutobj

License

MIT License - 详见 LICENSE

Project details


Download files

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

Source Distribution

mutobj-0.2.1.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

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

mutobj-0.2.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file mutobj-0.2.1.tar.gz.

File metadata

  • Download URL: mutobj-0.2.1.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mutobj-0.2.1.tar.gz
Algorithm Hash digest
SHA256 784f4ef2854c6b93230f28dcdd6d6d9c6f7c914ef3ffd46a4368c15556c8302d
MD5 4c46f95f0f8c311494f7cb0919f33c82
BLAKE2b-256 6a9556f01f3859d17af27352246bffe8c30eb59543903bdd6dc455dc9febcab9

See more details on using hashes here.

Provenance

The following attestation bundles were made for mutobj-0.2.1.tar.gz:

Publisher: release.yml on tiwb/mutobj

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mutobj-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: mutobj-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mutobj-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5a2f7c7aaced79c700e97420d1630d8453630b4fadea3aa6abf100dc816ff0a
MD5 cb1cc701f9fea9e903a9aad03d66007c
BLAKE2b-256 41596d4dd71f3878c0d2bcd46b3668c565083a640bec99cbb6266939323737cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for mutobj-0.2.1-py3-none-any.whl:

Publisher: release.yml on tiwb/mutobj

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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