首页 > 解决方案 > 使用插槽时要 dict 的数据类

问题描述

如果我使用常规数据类,我可以得到一个 dict

@dataclass
class Cat:
   fur: bool
   meow: int

Cat(True, 3).__dict__

但是使用的时候就不行了__slots__。什么是最有效的解决方案?

@dataclass
class Cat:
   __slots___ = ['fur', 'meow']
   fur: bool
   meow: int

# Doesn't work:
Cat(True, 3).__dict__

标签: pythondictionaryslots

解决方案


推荐阅读