为什么我无法删除(移除)Python中静态列表的明文'existing instance'?
Why I can't delete (remove) the clearly 'existing instance' of the static list in Python?
我的目的如下:
如果创建了新实例,则必须将该实例添加到静态列表中。当创建的实例被删除时,它必须从静态列表中删除
并且终结器方法必须使用'del()'。
我遇到问题的 python 代码如下:
class Staff:
staffs = [] # static customer list
def __init__(self, name):
self.name = name
print("A staff is created!")
def __del__(self):
Staff.staffs.remove(self)
print("A staff is deleted!")
if __name__ == '__main__':
# create a staff
Staff.staffs.append(Staff("Harry"))
# delete a staff
Staff.staffs[0].__del__()
错误如下:
异常被忽略:del at 0x0000028A3675EF70>
回溯(最后一次调用):
del 中的文件“(Path)\temp.py”,第 11 行
Staff.staffs.remove(自己)
ValueError: list.remove(x): x 不在列表中
而且我可以检查创建的实例是否存在于静态列表中,如下面的代码
def check(self):
print("Length of Static List:", len(Staff.staffs))
print("self", self)
for a_staff in Staff.staffs:
if a_staff == self:
print("a_staff", a_staff)
print()
结果:
静态列表长度:1
self <main.Staff 对象位于 0x000001FF7506CFD0>
a_staff <main.0x000001FF7506CFD0 处的工作人员对象>
实例的内存地址完全一样,但不知道为什么解释器说静态列表中没有实例。
当我运行类似的代码在Java时没有问题。
为什么会出现这个错误?
注意
class Staff:
staffs = [] # static customer list
def __init__(self, name):
self.name = name
print("A staff is created!")
def delete(self):
Staff.staffs.remove(self)
print("A staff is deleted!")
if __name__ == '__main__':
# create a staff
Staff.staffs.append(Staff("Harry"))
# delete a staff
Staff.staffs[0].delete()
工作正常。我所做的只是重命名__del__
删除。您可以尝试解释上面的 documentation 注释
Due to the precarious circumstances under which del() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead.
以上引用解释了您的错误。但是,正如 other posts 大纲,__del__
是一个
really low-level method which you don't need 99.9% of the time
所以只使用上面的代码就足够了,它会将您的实例标记为垃圾回收。
如果必须向 __del__
命令添加其他内容,请尝试以下操作:
class Staff:
staffs = [] # static customer list
def __init__(self, name):
self.name = name
print("A staff is created!")
def __del__(self):
print("A staff is deleted!")
if __name__ == '__main__':
# create a staff
Staff.staffs.append(Staff("Harry"))
print(f"The length of Staff before delete is: {len(Staff.staffs)}")
# delete a staff
del Staff.staffs[0]
print(f"The length of Staff after delete is: {len(Staff.staffs)}")
这输出:
A staff is created!
The length of Staff before delete is: 1
A staff is deleted!
The length of Staff after delete is: 0
所以项目被删除,你的文字被打印出来,无一例外!
我的目的如下: 如果创建了新实例,则必须将该实例添加到静态列表中。当创建的实例被删除时,它必须从静态列表中删除
并且终结器方法必须使用'del()'。
我遇到问题的 python 代码如下:
class Staff:
staffs = [] # static customer list
def __init__(self, name):
self.name = name
print("A staff is created!")
def __del__(self):
Staff.staffs.remove(self)
print("A staff is deleted!")
if __name__ == '__main__':
# create a staff
Staff.staffs.append(Staff("Harry"))
# delete a staff
Staff.staffs[0].__del__()
错误如下:
异常被忽略:
回溯(最后一次调用): del 中的文件“(Path)\temp.py”,第 11 行 Staff.staffs.remove(自己)
ValueError: list.remove(x): x 不在列表中
而且我可以检查创建的实例是否存在于静态列表中,如下面的代码
def check(self):
print("Length of Static List:", len(Staff.staffs))
print("self", self)
for a_staff in Staff.staffs:
if a_staff == self:
print("a_staff", a_staff)
print()
结果:
静态列表长度:1
self <main.Staff 对象位于 0x000001FF7506CFD0>
a_staff <main.0x000001FF7506CFD0 处的工作人员对象>
实例的内存地址完全一样,但不知道为什么解释器说静态列表中没有实例。
当我运行类似的代码在Java时没有问题。
为什么会出现这个错误?
注意
class Staff:
staffs = [] # static customer list
def __init__(self, name):
self.name = name
print("A staff is created!")
def delete(self):
Staff.staffs.remove(self)
print("A staff is deleted!")
if __name__ == '__main__':
# create a staff
Staff.staffs.append(Staff("Harry"))
# delete a staff
Staff.staffs[0].delete()
工作正常。我所做的只是重命名__del__
删除。您可以尝试解释上面的 documentation 注释
Due to the precarious circumstances under which del() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead.
以上引用解释了您的错误。但是,正如 other posts 大纲,__del__
是一个
really low-level method which you don't need 99.9% of the time
所以只使用上面的代码就足够了,它会将您的实例标记为垃圾回收。
如果必须向 __del__
命令添加其他内容,请尝试以下操作:
class Staff:
staffs = [] # static customer list
def __init__(self, name):
self.name = name
print("A staff is created!")
def __del__(self):
print("A staff is deleted!")
if __name__ == '__main__':
# create a staff
Staff.staffs.append(Staff("Harry"))
print(f"The length of Staff before delete is: {len(Staff.staffs)}")
# delete a staff
del Staff.staffs[0]
print(f"The length of Staff after delete is: {len(Staff.staffs)}")
这输出:
A staff is created!
The length of Staff before delete is: 1
A staff is deleted!
The length of Staff after delete is: 0
所以项目被删除,你的文字被打印出来,无一例外!