知识卡片

结构图卡 · 5841

验证组件通过协作调用组合

理解等级:中级

Typed检查类型,Unsigned检查小于零,最后Descriptor保存值;UnsignedInteger把Integer与Unsigned合并,super按MRO串联验证后存储。

结构图:

classDiagram
 class Descriptor {
  +__init__(name=None, **opts)
  +__set__(instance, value)
 }
 class Typed {
  +__set__(instance, value)
 }
 class Unsigned {
  +__set__(instance, value)
 }
 Descriptor <|-- Typed
 Descriptor <|-- Unsigned
 Typed <|-- Integer
 Integer <|-- UnsignedInteger
 Unsigned <|-- UnsignedInteger

箭头均为继承。共同签名与每层正确调用super是组合前提,类变量expected_type提供类型参数,不能凭多重继承自动证明任意组合顺序都正确。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • · 查看原书依据

      class UnsignedInteger(Integer, Unsigned): pass class Float(Typed): expected_type = float class UnsignedFloat(Float, Unsigned): pass cla

    参考来源

    - 位置:《Python Cookbook(第3版)中文版》8.13 实现一种数据模型或类型系统(spine16;extracted/Python Cookbook(第3版)中文版/_epub-src/OEBPS/Text/part0014.xhtml;u140-ch08-025)。 - 结论依据:完整读取本条问题、方案、代码与讨论;结论按真实控制流和适用前提表达,额外边界明确为解释或推导,示例未执行。 - 原始内容:class UnsignedInteger(Integer, Unsigned): pass class Float(Typed): expected_type = float class UnsignedFloat(Float, Unsigned): pass cla