Hướng dẫn delete object python - xóa đối tượng python

Tôi có một lớp hẹn giờ, mà tôi đã thiết kế để bắn một lần và (tối ưu) tự xóa. Có cách nào để tôi thực hiện sự tự phân hủy đó không?

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")

hỏi ngày 25 tháng 3 năm 2020 lúc 2:43Mar 25, 2020 at 2:43

Hướng dẫn delete object python - xóa đối tượng python

1

Bạn có thể làm theo cách tiếp cận tương tự.

Một

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
2 có thể được gọi để phá hủy một ví dụ.

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

Đối tượng không bị xóa trừ khi tất cả các tài liệu tham khảo của nó bị xóa

Ngoài ra, từ tài liệu tham khảo tài liệu chính thức của Python:

Del X doesn trực tiếp gọi x.del () - số lần giảm trước đây là số tham chiếu cho x từng cái và cái sau chỉ được gọi khi số tham chiếu của X đạt đến 0del() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero

Những gì bạn cần trong giải pháp của bạn là sử dụng một cái gì đó như

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
3 trong đó đối tượng là thể hiện bạn muốn xóa.

Đã trả lời ngày 25 tháng 3 năm 2020 lúc 2:48Mar 25, 2020 at 2:48

AzyCrw4282AzyCrw4282AzyCrw4282

6.6045 Huy hiệu vàng16 Huy hiệu bạc 30 Huy hiệu Đồng5 gold badges16 silver badges30 bronze badges

4

Bạn đã thử sử dụng không? một cái gì đó như thế này:

timer = None

Đã trả lời ngày 25 tháng 3 năm 2020 lúc 4:19Mar 25, 2020 at 4:19

Chadee Fouadchadee FouadChadee Fouad

2.1532 huy hiệu vàng20 Huy hiệu bạc22 Huy hiệu đồng2 gold badges20 silver badges22 bronze badges

Từ khóa ____14 trong Python chủ yếu được sử dụng để xóa các đối tượng trong Python. Vì mọi thứ trong Python đại diện cho một đối tượng theo cách này hay cách khác, từ khóa

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5 cũng có thể được sử dụng để xóa danh sách, cắt danh sách, xóa từ điển, xóa các cặp giá trị khóa khỏi từ điển, xóa các biến, v.v.
>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
4
keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The
>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5 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

Dưới đây là các ví dụ khác nhau hiển thị các trường hợp sử dụng khác nhau của từ khóa ____14:

1. Từ khóa Del để xóa các đối tượng

Ví dụ: Trong chương trình dưới đây, chúng tôi sẽ xóa Sample_Class bằng cách sử dụng ____17Statement.
In the program below we will deleted Sample_class using

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

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

timer = None
0
timer = None
1
timer = None
2
timer = None
3

timer = None
0____25
timer = None
6
timer = None
7
timer = None
8

timer = None
9

Syntax: del object_name
0

Syntax: del object_name
1

Syntax: del object_name
2

Syntax: del object_name
3


Syntax: del object_name
0

Syntax: del object_name
5

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

Syntax: del object_name
7


Syntax: del object_name
0

Syntax: del object_name
5

Output:

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

1. Từ khóa Del để xóa các biến

Ví dụ: Trong chương trình dưới đây, chúng tôi sẽ xóa một biến bằng cách sử dụng từ khóa ____14.
In the program below we will delete a variable using

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

class '__main__.Sample_class'
1
timer = None
2
timer = None
3

class '__main__.Sample_class'
4
timer = None
2

Syntax: del object_name
2


Syntax: del object_name
0
class '__main__.Sample_class'
8


Syntax: del object_name
0
NameError:name 'Sample_class' is not defined
0

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5
NameError:name 'Sample_class' is not defined
2

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5
NameError:name 'Sample_class' is not defined
4


Syntax: del object_name
0
class '__main__.Sample_class'
8


Syntax: del object_name
0
NameError:name 'Sample_class' is not defined
0

Output:

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

1. Từ khóa DEL để xóa danh sách và danh sách cắt

Ví dụ: Trong chương trình bên dưới, chúng tôi sẽ xóa một danh sách và cắt một danh sách khác bằng cách sử dụng từ khóa ____14.
In the program below we will delete a list and slice another list using

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

Các

[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]
1
timer = None
2
20
GeeksForGeeks
20
2
[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]
4
20
GeeksForGeeks
20
4
[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]
6
20
GeeksForGeeks
20
4
[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]
8
[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]
0


Syntax: del object_name
0
NameError: name 'my_list2' is not defined
1


Syntax: del object_name
0
NameError: name 'my_list2' is not defined
3

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5
NameError: name 'my_list2' is not defined
5
20
GeeksForGeeks
20
3
[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]
0


Syntax: del object_name
0
NameError: name 'my_list2' is not defined
1

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5
NameError: name 'my_list2' is not defined
5
20
GeeksForGeeks
20
7
>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
03
NameError: name 'my_variable2' is not defined
1
[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]
0


Syntax: del object_name
0
NameError: name 'my_list2' is not defined
1

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


Syntax: del object_name
0
NameError: name 'my_list2' is not defined
3

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. Từ khóa DEL để xóa từ điển và xóa các cặp giá trị khóa

Ví dụ: Trong chương trình dưới đây, chúng tôi sẽ xóa một từ điển và xóa một vài cặp giá trị khóa bằng cách sử dụng từ khóa ____14.
In the program below we will delete a dictionary and remove few key-value pairs using

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

Is

Các


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


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

>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
5
>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
48
>>> class Test:
...     def __del__(self):
...         print "deleted"
... 
>>> test = Test()
>>> del test
deleted
20
[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]
0


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

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


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

Output:

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

Vui lòng tham khảo delattr () và del () để biết thêm chi tiết.