Home > Software engineering >  How to delete all variables after finishing the execution in Python GUI?
How to delete all variables after finishing the execution in Python GUI?

Time:10-07

My GUI program which is written by PyQt5, has 4 tabs which I run the other tab after finishing the current tab. For example execute Tab1 and after saving the results i go to second tab and run that tab. My question is that after finishing the execution of the current tab, how can i delete all variables which i have created during the execution of this tab. I mean deleting variables from the RAM. Does using gc.collect at the end of that tab remove the variables? Any answer would be very helpful for me.

CodePudding user response:

You can try del statement

Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. If the name is unbound, a NameError exception will be raised.

a = 1
b = 2
print(a)
print(b)
del a, b
print(b)


# NameError: name 'b' is not defined

CodePudding user response:

I don't think there's a way to delete all of them built-in so I would add them to a var list when declaring them and delete all of them at once when ready.

    varlist = []
    
    myvar1 = 'string'
    varlist.append(myvar1)

    myvar2 = 123
    varlist.append(myvar2)

    for var in varlist:
        del var
  • Related