如何创建只读插槽?

问题描述

插槽可写

df1 <- structure(list(Name = c("Baron Davis","Baron Davis","Jason Kidd","Kevin Durant","Michael Jordan","Reggie Miller","Allan Houston","Allan Houston"),Teams = c("Hornets","Warriors","Knicks","Suns","Nets","Mavs","Thunder","Bulls","Pacers","Pistons")),class = "data.frame",row.names = c(NA,-14L))

如何创建只读槽,例如 >>> class A: __slots__ = ('x',) ... >>> list(vars(A)) ['__module__','__slots__','x','__doc__'] >>> vars(A)['x'] <member 'x' of 'A' objects> >>> a = A() >>> a.x = 'foo' >>> del a.x 类的槽 '__thisclass__''__self__''__self_class__'?>

super

解决方法

您不能,Python 代码没有选项可以创建只读描述符,例如用于 __thisclass__ 等的描述符。

在 C API 中,槽都使用相同的描述符对象类型,对于 PyMemberDef arrays 中将 flags 设置为 READONLY 的条目,它们变为只读。

例如您确定的 super 描述符在 super_members array 中定义:

static PyMemberDef super_members[] = {
    {"__thisclass__",T_OBJECT,offsetof(superobject,type),READONLY,"the class invoking super()"},{"__self__",obj),"the instance invoking super(); may be None"},{"__self_class__",obj_type),"the type of the instance invoking super(); may be None"},{0}
};

当处理__slots__时,没有可以设置这个标志的路径; code in type.__new__ just sets the first three values,分别为 nametypeoffset

    mp = PyHeapType_GET_MEMBERS(et);
    slotoffset = base->tp_basicsize;
    if (et->ht_slots != NULL) {
        for (i = 0; i < nslots; i++,mp++) {
            mp->name = PyUnicode_AsUTF8(
                PyTuple_GET_ITEM(et->ht_slots,i));
            if (mp->name == NULL)
                goto error;
            mp->type = T_OBJECT_EX;
            mp->offset = slotoffset;


            /* __dict__ and __weakref__ are already filtered out */
            assert(strcmp(mp->name,"__dict__") != 0);
            assert(strcmp(mp->name,"__weakref__") != 0);


            slotoffset += sizeof(PyObject *);
        }
    }

供参考:

  • PyHeapType_GET_MEMBERS 访问正在创建的新类型对象的 PyMemberDef 数组。它已经分配了正确数量的插槽。
  • et->ht_slots 是插槽名称的元组。
  • slotoffset 是实例对象内存区域的相对偏移量,用于存储槽内容。
  • T_OBJECT_EX 字段的 type 值表示该槽存储了一个指向 Python 对象的指针,如果该指针设置为 NULL,则会引发 AttributeError你试图获得价值。

请注意,如果可以将这些插槽设置为只读,那么您还需要一种机制来在创建新实例之前提供它们的值!毕竟,如果所有 Python 代码都无法在实例上设置只读属性,那么您的 Python 类将如何设置初始值?