[Python] Why am I getting "NameError: global name 'open' is not defined" in __del__?

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm getting a NameError in the __del__ function of a class. I do not understand why 'open' is not accessible inside the function __del__. I am using Python 3.4.0

    Python Code:

    class Contoller:

    ...

    def __del__(self):
    store = {}
    ...
    pickle.dump(store, open('data.p', 'wb'))

    class MyWindow(Gtk.Window):

    def __init__(self):
    ...
    self.controller = Contoller(self)
    ...
    self.connect("delete-event", self.quit)
    ...

    ...

    def quit(self, widget, event):
    del self.controller
    Gtk.main_quit()


    Error Message:

    Traceback (most recent call last):
    File "main.py", line 69, in __del__
    NameError: name 'open' is not defined

    Continue reading...

Compartilhe esta Página