Hướng dẫn python delete object

I have a timer class, that I designed to fire once, and (optimally) delete itself. Is there a way for me to implement that self-deletion?

class timer:
        def __init__(self, duration, function, args):
            self.duration = duration
            self.function = function
            self.args = args
        def start(self):
            self.time = time.time()
            self.validity = True    
        def check(self):
            if(int(self.time)+self.duration < time.time() ):
                self.function(self.args)
                self.validity = False
        def __del__(self):
            print("timer destroyed")

asked Mar 25, 2020 at 2:43

Hướng dẫn python delete object

1

You can follow the similar approach.

An object.__del__(self) can be called to destroy an instance.

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted

Object is not deleted unless all of its references are removed

Also, From Python official doc reference:

del x doesn’t directly call x.del() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero

What you would need in your solution is to use something like del object where the object is the instance that you want to remove.

answered Mar 25, 2020 at 2:48

AzyCrw4282AzyCrw4282

6,6045 gold badges16 silver badges30 bronze badges

4

Did you try using None? something like this:

timer = None

answered Mar 25, 2020 at 4:19

Chadee FouadChadee Fouad

2,1532 gold badges20 silver badges22 bronze badges

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.


Syntax: del object_name

Below are various examples that show-case various use-cases of the del keyword:

1. del keyword for deleting objects

Example:
In the program below we will deleted Sample_class using del Sample_class statement.

class Sample_class:

    some_variable = 20

    def my_method(self):

        print("GeeksForGeeks")

print(Sample_class)

del Sample_class

print(Sample_class)

Output:

class '__main__.Sample_class'
NameError:name 'Sample_class' is not defined

1. del keyword for deleting variables

Example:
In the program below we will delete a variable using del keyword.

my_variable1 = 20

my_variable2 = "GeeksForGeeks"

print(my_variable1)

print(my_variable2)

del my_variable1

del my_variable2

print(my_variable1)

print(my_variable2)

Output:

20
GeeksForGeeks
20
NameError: name 'my_variable2' is not defined

1. del keyword for deleting list and list slicing

Example:
In the program below we will delete a list and slice another list using del keyword.

my_list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

my_list2 =["Geeks", "For", "Geek"]

print(my_list1)

print(my_list2)

del my_list1[1]

print(my_list1)

del my_list1[3:5]

print(my_list1)

del my_list2

print(my_list2)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
['Geeks', 'For', 'Geek']
[1, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 4, 7, 8, 9]
NameError: name 'my_list2' is not defined

1. del keyword for deleting dictionaries and removing key-value pairs

Example:
In the program below we will delete a dictionary and remove few key-value pairs using del keyword.

my_dict1 = {"small": "big", "black": "white", "up": "down"}

my_dict2 = {"dark": "light", "fat": "thin", "sky": "land"}

print(my_dict1)

print(my_dict2)

del my_dict1["black"]

print(my_dict1)

del my_dict2

print(my_dict2)

Output:

{'small': 'big', 'black': 'white', 'up': 'down'}
{'dark': 'light', 'fat': 'thin', 'sky': 'land'}
{'small': 'big', 'up': 'down'}
NameError: name 'my_dict2' is not defined

Please refer delattr() and del() for more details.