Trong bài viết này, chúng ta sẽ thấy các hàm delattr [] và del [] trong Python
delattr [] trong python
Phương thức delAttr [] được sử dụng để xóa thuộc tính được đặt tên khỏi đối tượng, với sự cho phép trước của đối tượng. is used to delete the named attribute from the object, with the prior permission of the object.
Syntax:
Delattr [đối tượng, tên]: Hàm chỉ lấy hai tham số:The function takes only two parameter:
- Đối tượng: & nbsp; từ đó & nbsp; thuộc tính tên sẽ được xóa. from which the name attribute is to be removed.
- Tên: & nbsp; của thuộc tính & nbsp; sẽ được xóa. of the attribute which is to be removed.
Trả về: Hàm không trả về bất kỳ giá trị nào.The function doesn’t return any value.
Công việc: Giả sử chúng ta có một lớp học theo tên Geek và nó có năm sinh viên là thuộc tính. Vì vậy, bằng cách sử dụng phương thức delAttr [], chúng ta có thể xóa bất kỳ một trong các thuộc tính.Suppose we have a class by named Geek, and it has five students as the attribute. So, using the delattr[] method, we can remove any one of the attributes.
Python3
class
Geek:
domain
____10
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain1
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain2
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain3
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain010
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain6
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain7
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain9____10
delattr[object, name]1
delattr[object, name]3
delattr[object, name]4
delattr[object, name]5
delattr[object, name]6
delattr[object, name]3
delattr[object, name]9
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
delattr[Coordinate, 'z']
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Error
print['z = ',point1.z]
1class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
delattr[Coordinate, 'z']
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Error
print['z = ',point1.z]
2class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
delattr[Coordinate, 'z']
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Error
print['z = ',point1.z]
3delattr[object, name]6
delattr[object, name]3
delattr[object, name]4
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
delattr[Coordinate, 'z']
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Error
print['z = ',point1.z]
8delattr[object, name]6
delattr[object, name]3
delattr[object, name]9
Output:
Traceback [most recent call last]: File "02d61301-4399-4354-b3c3-86641ba21460.py", line 9, in delattr[geeks, "domain"] AttributeError: domain
Giải thích: Đối tượng của Geek lớp đang có một thuộc tính có tên là miền. Nếu chúng ta sử dụng hàm delattr [] trên đối tượng Geek của lớp Geek cho thuộc tính miền, thì nó sẽ không còn tên miền thuộc tính nữa. Trong trường hợp đó, nếu chúng ta muốn truy cập thuộc tính tên miền, nó sẽ tăng một thuộc tính. The object of the class Geek is having one attribute named domain. If we use delattr[] function on the geeks object of the Geeks class for the domain attribute, then it’ll no more have the attribute domain. In that case, if we want to access the domain attribute, it will raise an AttributeError.
Nhà điều hành Del trong Python
Có một toán tử khác trong Python thực hiện công việc tương tự như phương thức delAttr []. Nó là toán tử DEL. Toán tử DEL thực hiện một số biến sẵn sàng cho bộ sưu tập rác, nếu không có tham chiếu về biến trong phần sau của luồng điều khiển chương trình.del operator. del operator makes some variable ready for garbage collection, if there is no reference of the variable in the later part of the program control flow.
Python3
class
Geek:
domain
____10
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain1
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain2
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain3
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain010
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain6
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain7
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain9____10
delattr[object, name]1
delattr[object, name]3
delattr[object, name]4
delattr[object, name]5
delattr[object, name]6
delattr[object, name]3
delattr[object, name]9
Giải thích: Đối tượng của Geek lớp đang có một thuộc tính có tên là miền. Nếu chúng ta sử dụng hàm delattr [] trên đối tượng Geek của lớp Geek cho thuộc tính miền, thì nó sẽ không còn tên miền thuộc tính nữa. Trong trường hợp đó, nếu chúng ta muốn truy cập thuộc tính tên miền, nó sẽ tăng một thuộc tính.
delattr[object, name]3
delattr[object, name]4
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
delattr[Coordinate, 'z']
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Error
print['z = ',point1.z]
8delattr[object, name]6
delattr[object, name]3
delattr[object, name]9
Output:
Nhà điều hành Del trong PythonCó một toán tử khác trong Python thực hiện công việc tương tự như phương thức delAttr []. Nó là toán tử DEL. Toán tử DEL thực hiện một số biến sẵn sàng cho bộ sưu tập rác, nếu không có tham chiếu về biến trong phần sau của luồng điều khiển chương trình.
Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain
2Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain
3Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain
010Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain
6Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain
7 del is more explicit and efficient, and delattr[] allows dynamic attribute deleting.class
8class
9If the above programs are considered and run, then there is a slight difference between the speed of execution. del is slightly faster in comparison to delattr[], depending on the machine.Before deleting domain attribute from geeks object: geeksforgeeks.org Traceback [most recent call last]: File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in del geeks.domain AttributeError: domain
del also takes less byte-code instructions in comparison to delattr[].
delattr [] và del [] trong python
DelAttr [] xóa một thuộc tính khỏi đối tượng [nếu đối tượng cho phép nó].
Cú pháp của Geek:
8 là:
delattr[object, name]
tham số delattr []
Geek:
8 lấy hai tham số:
- đối tượng - đối tượng mà thuộc tính tên sẽ bị xóa - the object from which name attribute is to be removed
- Tên - & nbsp; một chuỗi phải là tên của thuộc tính sẽ bị xóa khỏi đối tượng - a string which must be the name of the attribute to be removed from the object
Trả về giá trị từ delattr []
Geek:
8 không trả về bất kỳ giá trị nào [trả về
1].Nó chỉ loại bỏ một thuộc tính [nếu đối tượng cho phép nó].
Ví dụ 1: Delattr [] hoạt động như thế nào?
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
delattr[Coordinate, 'z']
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Error
print['z = ',point1.z]
Đầu ra
x = 10 y = -5 z = 0 --After deleting z attribute-- x = 10 y = -5 Traceback [most recent call last]: File "python", line 19, in AttributeError: 'Coordinate' object has no attribute 'z'
Ở đây, thuộc tính Z được xóa khỏi lớp tọa độ bằng cách sử dụng
2.
Ví dụ 2: Xóa thuộc tính bằng toán tử DEL
Bạn cũng có thể xóa thuộc tính của một đối tượng bằng toán tử DEL.
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate[]
print['x = ',point1.x]
print['y = ',point1.y]
print['z = ',point1.z]
# Deleting attribute z
del Coordinate.z
print['--After deleting z attribute--']
print['x = ',point1.x]
print['y = ',point1.y]
# Raises Attribute Error
print['z = ',point1.z]
Đầu ra của chương trình sẽ giống như trên.