For an example, I have a class A:
def start():
global object
object = A()
def stop():
del object
I got an error local variable 'object' referenced before assignment
I want to store a reference of the newly created object using the start function and delete the object reference using the stop function.
Is there any way to instantiate an object and delete an object instance using functions?
Thank you very much!
deldoesn't delete objects to begin withobject(orlistordictor...) - having said which, yourstoproutine hasn't been told that there is aglobalit should be looking at.globalinstop. But be aware, using global mutable state like this is usually considered bad designdelis rarely needed. It doesn't delete an object; it removes a name from a scope (or invokes__delitem__or__delattr__in the case ofdel x[i]ordel x.i). An object may be deleted, if you removed the last reference to the object.