class Book: def __init__(self, title, book_id, price): self.title = title self.book_id = book_id self.price = price def __del__(self): print(f"Book destroyed-{self.title},{self.book_id},{self.price}") if __title="__main__": book1 = Book("Python入门", "978-7-121-33620-1", 39.99) del book1改正错误
时间: 2023-12-31 10:07:14 浏览: 66
Python RuntimeError: thread.__init__() not called解决方法
您需要将 `if __title="__main__":` 改为 `if __name__=="__main__":`,因为 `__name__` 是一个特殊变量,用来表示当前模块的名字。当模块被直接运行时,`__name__` 的值为`__main__`,因此需要使用这个条件语句来判断是否在主程序中执行。修改后的代码如下:
```
class Book:
def __init__(self, title, book_id, price):
self.title = title
self.book_id = book_id
self.price = price
def __del__(self):
print(f"Book destroyed-{self.title},{self.book_id},{self.price}")
if __name__ == "__main__":
book1 = Book("Python入门", "978-7-121-33620-1", 39.99)
del book1
```
阅读全文