__slot__ 属性在对象的本机内存表示中分配,然后与访问的类关联的描述符实际上使用 CPython 中的本机 C 方法来设置和检索对归因于每个插槽属性的 Python 对象的引用在类实例上作为 C 结构。
在 Python 中以名称 member_descriptor 表示的插槽描述符在此处定义:https://github.com/python/cpython/blob/master/Objects/descrobject.c
如果不使用 CType 与本机代码交互,无论如何您都无法从纯 Python 代码执行或增强这些描述符。
可以通过执行类似的操作来获取他们的类型
class A:
__slots__ = "a"
member_descriptor = type(A.a)
然后可以假设可以从它继承,并编写派生的 __get__ 和 __set__ 方法来执行检查等 - 但不幸的是,它不能作为基类工作。
但是,可以编写其他并行的描述符,这些描述符又可以调用本机描述符来实际存储值。
通过使用元类,可以在类创建时重命名传入的 __slots__ 并将其访问权限包装在可以执行额外检查的自定义描述符中 - 甚至可以从“dir”中隐藏。
因此,对于一个简单的类型检查槽变体元类,可以有
class TypedSlot:
def __init__(self, name, type_):
self.name = name
self.type = type_
def __get__(self, instance, owner):
if not instance:
return self
return getattr(instance, "_" + self.name)
def __set__(self, instance, value):
if not isinstance(value, self.type):
raise TypeError
setattr(instance, "_" + self.name, value)
class M(type):
def __new__(metacls, name, bases, namespace):
new_slots = []
for key, type_ in namespace.get("__slots__", {}).items():
namespace[key] = TypedSlot(key, type_)
new_slots.append("_" + key)
namespace["__slots__"] = new_slots
return super().__new__(metacls, name, bases, namespace)
def __dir__(cls):
return [name for name in super().__dir__() if name not in cls.__slots__]