Xóa một chức năng không thực sự là điều bạn làm cho chính chức năng; Đó là điều bạn làm với không gian tên mà nó sống. [Giống như xóa số 3 khỏi danh sách không phải là thứ bạn làm cho số 3, đó là điều bạn làm trong danh sách.]
Giả sử bạn nói
def foo[x]: return 1
bar = foo
Sau đó [nhiều hoặc ít hơn] bạn có hai tên,
Syntax: del object_name0 và
Syntax: del object_name1, cho cùng một hàm. Bây giờ giả sử bạn gọi
Syntax: del object_name2 hoặc
Syntax: del object_name3. Điều tương tự chính xác, cụ thể là một đối tượng chức năng, đang được chuyển sang
Syntax: del object_name4. Nhưng những gì bạn thực sự muốn xóa là sự liên kết giữa tên
Syntax: del object_name0 hoặc
Syntax: del object_name1 và đối tượng đó - và không có cách nào có thể
Syntax: del object_name4 [tuy nhiên bạn xác định nó] của.
.
Vì thế. Các lựa chọn của bạn như sau. [1] Những điều hacky khó chịu, như vừa đề cập. Đừng. . Điều này là xấu xí và vô duyên. [3] Đừng bận tâm.
Tôi thực sự khuyên bạn nên số 3, trừ khi lý do duy nhất bạn làm điều này là để tìm hiểu thêm về cách thức hoạt động của Python. Trong trường hợp sau, một nơi tốt để bắt đầu có thể là //docs.python.org/reference/executionmodel.html.
Từ khóa ____22 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'3 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'2 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'3 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 ____22:
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 ____25Statement.
In
the program below we will deleted Sample_class using
class '__main__.Sample_class'5statement.
class '__main__.Sample_class'6
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
class '__main__.Sample_class'8
NameError:name 'Sample_class' is not defined3
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
NameError:name 'Sample_class' is not defined8
20 GeeksForGeeks 203
class '__main__.Sample_class'3
20 GeeksForGeeks 205
NameError:name 'Sample_class' is not defined8
20 GeeksForGeeks 203
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 ____22.
In the program below we
will delete a variable using
class '__main__.Sample_class'2 keyword.
20 GeeksForGeeks 209
NameError:name 'Sample_class' is not defined0
NameError:name 'Sample_class' is not defined1
NameError: name 'my_variable2' is not defined2
NameError:name 'Sample_class' is not defined0
20 GeeksForGeeks 200
NameError:name 'Sample_class' is not defined8
NameError: name 'my_variable2' is not defined6
NameError:name 'Sample_class' is not defined8
NameError: name 'my_variable2' is not defined8
class '__main__.Sample_class'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
class '__main__.Sample_class'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]2
NameError:name 'Sample_class' is not defined8
NameError: name 'my_variable2' is not defined6
NameError:name 'Sample_class' is not defined8
NameError: name 'my_variable2' is not defined8
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 ____22.
In the program below we will delete a list and slice another
list using
class '__main__.Sample_class'2 keyword.
Các
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}9
NameError:name 'Sample_class' is not defined0
NameError: name 'my_list2' is not defined0
NameError: name 'my_dict2' is not defined2
NameError: name 'my_list2' is not defined2
NameError: name 'my_dict2' is not defined4
NameError: name 'my_list2' is not defined2
NameError: name 'my_dict2' is not defined6
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}8
NameError:name 'Sample_class' is not defined8
NameError: name 'my_dict2' is not defined9
NameError:name 'Sample_class' is not defined8
Syntax: del object_name01
class '__main__.Sample_class'3
Syntax: del object_name03
NameError: name 'my_list2' is not defined1
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}8
NameError:name 'Sample_class' is not defined8
NameError: name 'my_dict2' is not defined9
Is
NameError:name 'Sample_class' is not defined8
NameError: name 'my_dict2' is not defined9
class '__main__.Sample_class'3
Syntax: del object_name17
NameError:name 'Sample_class' is not defined8
Syntax: del object_name01
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 ____22.
In the program below we will delete a dictionary and remove few key-value pairs using
class '__main__.Sample_class'2 keyword.
Syntax: del object_name21
NameError:name 'Sample_class' is not defined0
Syntax: del object_name23
Syntax: del object_name24
Syntax: del object_name25
Syntax: del object_name26__72
Syntax: del object_name36
NameError:name 'Sample_class' is not defined0
Syntax: del object_name23
Syntax: del object_name39
Syntax: del object_name25
Syntax: del object_name41
NameError: name 'my_list2' is not defined2
Syntax: del object_name43
Syntax: del object_name25
Syntax: del object_name45__72
NameError:name 'Sample_class' is not defined8
Syntax: del object_name52
NameError:name 'Sample_class' is not defined8
Syntax: del object_name54
class '__main__.Sample_class'3
Syntax: del object_name56
Syntax: del object_name28
{'small': 'big', 'black': 'white', 'up': 'down'} {'dark': 'light', 'fat': 'thin', 'sky': 'land'} {'small': 'big', 'up': 'down'}8
NameError:name 'Sample_class' is not defined8
Syntax: del object_name52
class '__main__.Sample_class'3
Syntax: del object_name62
NameError:name 'Sample_class' is not defined8
Syntax: del object_name54
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.