from selenium import webdriver
class Manager:
def __init__(self):
option = webdriver.ChromeOptions()
option.add_argument('headless')
self.driver = webdriver.Chrome(options=option)
self.driver.set_window_size(1700,1000)
def __del__(self):
print("delete object")
self.driver.quit()
if __name__ == '__main__':
import chromedriver_autoinstaller
chromedriver_autoinstaller.install(True)
a = Manager()
print('a')
del a
input()
destructor is not called
Does anyone know about this problem??
I want to delete the object
del
does not destroy any objects, it simply unlinks a reference. If other live references to the Manager
object still exist, or if the garbage collector has not run to clear up any cyclic references, __del__
is not called.
If you want to guarantee some kind of cleanup action, your best bet is using context managers:
class Manager:
def __init__(self):
option = webdriver.ChromeOptions()
option.add_argument('headless')
self.driver = webdriver.Chrome(options=option)
self.driver.set_window_size(1700,1000)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
print("exit context manager")
self.driver.quit()
You can then use it like so:
with Manager() as a:
print('a')
input()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.