Từ khóa ____13 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 '__main__.Sample_class'4 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 '__main__.Sample_class'3 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 '__main__.Sample_class'4 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 ____13:
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 ____16Statement.
In
the program below we will deleted Sample_class using
class '__main__.Sample_class'6statement.
class '__main__.Sample_class'7
class '__main__.Sample_class'8
class '__main__.Sample_class'9
NameError:name 'Sample_class' is not defined0
NameError:name 'Sample_class' is not defined1
NameError:name 'Sample_class' is not defined2
class '__main__.Sample_class'9
NameError:name 'Sample_class' is not defined4
NameError:name 'Sample_class' is not defined5
NameError:name 'Sample_class' is not defined6
NameError:name 'Sample_class' is not defined7
NameError:name 'Sample_class' is not defined8
NameError:name 'Sample_class' is not defined9
20 GeeksForGeeks 200
20 GeeksForGeeks 201
20 GeeksForGeeks 202
NameError:name 'Sample_class' is not defined9
20 GeeksForGeeks 204
class '__main__.Sample_class'4
20 GeeksForGeeks 206
NameError:name 'Sample_class' is not defined9
20 GeeksForGeeks 204
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 ____13.
In the program below we
will delete a variable using
class '__main__.Sample_class'3 keyword.
NameError: name 'my_variable2' is not defined0
NameError:name 'Sample_class' is not defined1
NameError:name 'Sample_class' is not defined2
NameError: name 'my_variable2' is not defined3
NameError:name 'Sample_class' is not defined1
20 GeeksForGeeks 201
NameError:name 'Sample_class' is not defined9
NameError: name 'my_variable2' is not defined7
NameError:name 'Sample_class' is not defined9
NameError: name 'my_variable2' is not defined9
class '__main__.Sample_class'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]1
class '__main__.Sample_class'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]3
NameError:name 'Sample_class' is not defined9
NameError: name 'my_variable2' is not defined7
NameError:name 'Sample_class' is not defined9
NameError: name 'my_variable2' is not defined9
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 ____13.
In the program below we will delete a list and slice another
list using
class '__main__.Sample_class'3 keyword.
Các
NameError: name 'my_dict2' is not defined0
NameError:name 'Sample_class' is not defined1
NameError: name 'my_list2' is not defined1
NameError: name 'my_dict2' is not defined3
NameError: name 'my_list2' is not defined3
NameError: name 'my_dict2' is not defined5
NameError: name 'my_list2' is not defined3
NameError: name 'my_dict2' is not defined7
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}9
NameError:name 'Sample_class' is not defined9
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
0NameError:name 'Sample_class' is not defined9
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
2class '__main__.Sample_class'4
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
4NameError: name 'my_list2' is not defined2
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}9
NameError:name 'Sample_class' is not defined9
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
0class '__main__.Sample_class'4
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
4NameError: name 'my_list2' is not defined6
class '__main__.Sample_class'022.
NameError:name 'Sample_class' is not defined9
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
0class '__main__.Sample_class'4
class '__main__.Sample_class'08
NameError:name 'Sample_class' is not defined9
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
2Output:
[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 ____13.
In the program below we will delete a dictionary and remove few key-value pairs using
class '__main__.Sample_class'3 keyword.
Is
Các
NameError:name 'Sample_class' is not defined9
class '__main__.Sample_class'43
NameError:name 'Sample_class' is not defined9
class '__main__.Sample_class'45
class '__main__.Sample_class'4
class '__main__.Sample_class'47
class '__main__.Sample_class'19
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}9
NameError:name 'Sample_class' is not defined9
class '__main__.Sample_class'43
class '__main__.Sample_class'4
class '__main__.Sample_class'53
NameError:name 'Sample_class' is not defined9
class '__main__.Sample_class'45
Output:
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}
NameError: name 'my_dict2' is not defined
Vui lòng tham khảo delattr [] và del [] để biết thêm chi tiết.
Việc loại bỏ [] làm gì trong Python?
if self.hit_paddle[pos] == True or self.hit_paddle2[pos] == True:
bar = bar + 1
if bar == 1:
global barbox1
barbox1 = barfill[canvas]
barbox1.canvas.move[barbox1.id, 253, 367]
if bar == 2:
global barbox2
barbox2 = barfill[canvas]
barbox2.canvas.move[barbox5.id, 413, 367]
bar = 0
time.sleep[0.2]
barbox1 = None
barbox2 = None
Đó là mã, điều chính tôi đã thử để xóa các đối tượng là
class '__main__.Sample_class'56, nhưng điều đó dường như không hoạt động.
bad_coder
9.43619 Huy hiệu vàng38 Huy hiệu bạc62 Huy hiệu Đồng19 gold badges38 silver badges62 bronze badges
hỏi ngày 2 tháng 2 năm 2014 lúc 18:43Feb 2, 2014 at 18:43
4
class '__main__.Sample_class'57 được gọi khi trường hợp sắp bị phá hủy.
class '__main__.Sample_class'0
Đố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 [như được trích dẫn bởi Ethan]
Ngoài ra, từ tài liệu tham khảo tài liệu chính thức của Python:
class '__main__.Sample_class'58 doesn trực tiếp gọiclass '__main__.Sample_class'59 - số lần giảm trước số lượng tham chiếu choclass '__main__.Sample_class'60 theo một và cái sau chỉ được gọi khi số lượng tham chiếu của ____ ____ 160
Ethan Furman
59,9K18 Huy hiệu vàng146 Huy hiệu bạc221 Huy hiệu đồng18 gold badges146 silver badges221 bronze badges
Đã trả lời ngày 2 tháng 2 năm 2014 lúc 19:14Feb 2, 2014 at 19:14
gitesh.tyagigitesh.tyagigitesh.tyagi
2.0431 Huy hiệu vàng13 Huy hiệu bạc20 Huy hiệu Đồng1 gold badge13 silver badges20 bronze badges
5
Ý bạn là gì bởi
class '__main__.Sample_class'62? Trong Python, việc xóa tham chiếu [hoặc tên] có thể được thực hiện bằng từ khóa
class '__main__.Sample_class'4, nhưng nếu có tên khác cho cùng một đối tượng mà đối tượng sẽ không bị xóa.
class '__main__.Sample_class'1
so với:
class '__main__.Sample_class'2
Đã trả lời ngày 2 tháng 2 năm 2014 lúc 19:22Feb 2, 2014 at 19:22
Ethan Furmanethan FurmanEthan Furman
59,9K18 Huy hiệu vàng146 Huy hiệu bạc221 Huy hiệu đồng18 gold badges146 silver badges221 bronze badges
3