犯罪嫌疑人X
我想扩展 Hkoof 提供的类变量解决方案,通过添加对实例的直接引用,这也有助于保持类一致的实例列表,允许访问上一个/下一个创建的实例。唯一的问题(实际上,其他解决方案也没有涵盖)是实例的删除需要显式的方法调用,这是del不够的:__del__仅在没有对对象的引用时才调用。由于显然无法知道用户/程序员是否保留了对它的任何引用,因此我们需要一种明确的方法来做到这一点;它不能确保实例将被垃圾收集,因为只有在没有对它的引用时才会发生这种情况。class NoteBlock(object): instancies = [] def __init__(self, id): self.id = id def __new__(cls, *args, **kwargs): instancy = object.__new__(cls) cls.instancies.append(instancy) return instancy def delete(self): self.instancies.remove(self) # the following property methods are only useful for "browsing" between the # instance list @property def previous(self): try: # "-1" index returns the last object in the instancies list, we # don't want that... previous_index = self.instancies.index(self) - 1 assert previous_index >= 0 return self.instancies[previous_index] except: return @property def next(self): try: return self.instancies[self.instancies.index(self) + 1] except: return# create some random objectsfrom random import randrangescope_instance_list = []print('Creating instancies:')for i in range(8): index = randrange(100) block = NoteBlock(index) scope_instance_list.append(block) print('\t{} - Block {} created'.format(i + 1, index))# remove a single instancetoRemoveIndex = randrange(8)toRemove = scope_instance_list.pop(toRemoveIndex)print('\nRemoving instance n. {} ({})...'.format(toRemoveIndex + 1, format(toRemove.id)))# we can't use "del", as the __del__ magic method only works as soon as there is# *no* reference left for the object: since we're keeping the "instancies" list# it will never be called, then we need to use an "internal" way to do that;# keep in mind that if you have *any* reference to that object, it will never be# garbage collected until it's "released".toRemove.delete()print('Done!\n')# show the current instance list, including previous and next instancies,# according to the existing objectsprint('Remaining instance list (class instance based):')for i, inst in enumerate(block.instancies): print('\t{} - Block {}: previous: {}, next: {}'.format( i + 1, inst.id, inst.previous.id if inst.previous else None, inst.next.id if inst.next else None))示例输出:Creating instancies: 1 - Block 10 created 2 - Block 23 created 3 - Block 4 created 4 - Block 28 created 5 - Block 9 created 6 - Block 67 created 7 - Block 70 created 8 - Block 73 createdRemoving instance n. 5 (9)...Done!Remaining instance list (class instance based): 1 - Block 10: previous: None, next: 23 2 - Block 23: previous: 10, next: 4 3 - Block 4: previous: 23, next: 28 4 - Block 28: previous: 4, next: 67 5 - Block 67: previous: 28, next: 70 6 - Block 70: previous: 67, next: 73 7 - Block 73: previous: 70, next: None