(continued from previous page) (continues on next page) 19 The$_{object.__new__()}$ method takes care of creating instances that have slots instead of an instance dictionary. Here is a rough simulation in pure Python: The$_{type.__new__()}$ method takes care of adding member objects to class variables: # Also see descr_new() in Objects/descrobject.c self.name =name self.clsname=clsname self.offset =offset def __get__ (self, obj, objtype = None ): 'Emulate member_get() in Objects/descrobject.c ' # Also see PyMember_GetOne() in Python/structmember.c value=obj._slotvalues[ self.offset] if value is null: raise AttributeError(self.name) return value def __set__ (self, obj, value): 'Emulate member_set() in Objects/descrobject.c ' obj._slotvalues[ self.offset] =value def __delete__ (self, obj): 'Emulate member_delete() in Objects/descrobject.c ' value=obj._slotvalues[ self.offset] if value is null: raise AttributeError(self.name) obj._slotvalues[ self.offset] =null def __repr__ (self): 'Emulate member_repr() in Objects/descrobject.c ' return f'<Member { self.name !r} of { self.clsname !r} >' classType (type): 'Simulate how the type metaclass adds member objects for slots ' def __new__ (mcls, clsname, bases, mapping): 'Emuluate type_new() in Objects/typeobject.c ' # type_new() calls PyTypeReady() which calls add_methods() slot_names=mapping.get('slot_names ', []) for offset, name in enumerate(slot_names): mapping[name]=Member(name, clsname, offset) return type.__new__ (mcls, clsname, bases, mapping) classObject : 'Simulate how object.__new__() allocates memory for __slots__ ' def __new__ (cls,*args): 'Emulate object_new() in Objects/typeobject.c ' inst =super() .__new__ (cls) if hasattr(cls,'slot_names '): empty_slots=[null]*len(cls.slot_names) object.__setattr__ (inst,'_slotvalues ', empty_slots) return inst def __setattr__ (self, name, value): 'Emulate _PyObject_GenericSetAttrWithDict() Objects/object.c ' cls=type(self) if hasattr(cls,'slot_names ') and name not in cls.slot_names: raise AttributeError( f' { type(self).__name__ !r} object has no attribute { name !r} '