Hướng dẫn how do you drop an attribute in python? - làm thế nào để bạn thả một thuộc tính trong python?

    Hướng dẫn Python
  • Python bình luậnComments
  • Hoạt động giao diện điều khiển PythonConsole Operations
  • Tuyên bố có điều kiện PythonConditional Statements
  • Các câu lệnh Vòng lặp PythonLoop Statements
  • Python enumEnum
  • Các nhà khai thác PythonOperators
  • Chức năng PythonFunctions
  • Chức năng xây dựng PythonBuiltin Functions
  • Chuyển đổi loại PythonConversion
  • Các lớp và đối tượng PythonClasses and Objects
  • Chức năng toán học PythonMath Functions

Để xóa một thuộc tính khỏi đối tượng Python, hãy gọi hàm tích hợp delattr () và chuyển tên đối tượng và thuộc tính làm đối số.

Câu lệnh chương trình sau đây là một ví dụ để gọi hàm delattr () để xóa thuộc tính với tên attrName từ đối tượng Python object.

delattr(object, attrName)

delattr () trả về không.

Ví dụ 1: Xóa thuộc tính khỏi đối tượng Python

Trong ví dụ này, chúng tôi sẽ lấy một đối tượng

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
0 với các thuộc tính
class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
1 và
class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
2. Sau đó, chúng tôi sẽ sử dụng hàm delattr () và xóa thuộc tính
class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
1.

Chương trình Python

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

Chạy

Đầu ra

Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True

Bản tóm tắt

Trong hướng dẫn này về các ví dụ Python, chúng tôi đã học cách xóa một thuộc tính khỏi một đối tượng Python.

Điều này có thể là ngớ ngẩn, nhưng nó đã cằn nhằn phía sau não của tôi trong một thời gian.

Python cung cấp cho chúng tôi hai cách tích hợp để xóa các thuộc tính khỏi các đối tượng, từ lệnh del và hàm tích hợp DelAttr. Tôi thích Delattr vì tôi nghĩ nó rõ ràng hơn một chút:del command word and the delattr built-in function. I prefer delattr because it I think its a bit more explicit:

del foo.bar
delattr(foo, "bar")

Nhưng tôi đang tự hỏi liệu có thể có sự khác biệt dưới sự hiểu biết giữa họ.

hỏi ngày 13 tháng 7 năm 2009 lúc 17:33Jul 13, 2009 at 17:33

Pydannypydannypydanny

7.6146 Huy hiệu vàng33 Huy hiệu bạc41 Huy hiệu đồng6 gold badges33 silver badges41 bronze badges

Thứ nhất là hiệu quả hơn so với thứ hai.

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
4 Biên dịch thành hai hướng dẫn mã byte:

  2           0 LOAD_FAST                0 (foo)
              3 DELETE_ATTR              0 (bar)

trong khi

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
5 mất năm:

  2           0 LOAD_GLOBAL              0 (delattr)
              3 LOAD_FAST                0 (foo)
              6 LOAD_CONST               1 ('bar')
              9 CALL_FUNCTION            2
             12 POP_TOP             

Điều này chuyển thành lần chạy đầu tiên nhanh hơn một chút (nhưng nó không phải là một sự khác biệt lớn - .15 trên máy của tôi).

Giống như những người khác đã nói, bạn thực sự nên chỉ sử dụng biểu mẫu thứ hai khi thuộc tính mà bạn đang xóa được xác định một cách linh hoạt.

[Đã chỉnh sửa để hiển thị các hướng dẫn bytecode được tạo bên trong một hàm, trong đó trình biên dịch có thể sử dụng

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
6 và
class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
7]]

Đã trả lời ngày 13 tháng 7 năm 2009 lúc 17:58Jul 13, 2009 at 17:58

Hướng dẫn how do you drop an attribute in python? - làm thế nào để bạn thả một thuộc tính trong python?

MilesmilesMiles

30,5K7 Huy hiệu vàng59 Huy hiệu bạc72 Huy hiệu đồng7 gold badges59 silver badges72 bronze badges

8

  • Del rõ ràng hơn và hiệu quả hơn; is more explicit and efficient;
  • DelAttr cho phép xóa thuộc tính động. allows dynamic attribute deleting.

Xem xét các ví dụ sau:

for name in ATTRIBUTES:
    delattr(obj, name)

or:

def _cleanup(self, name):
    """Do cleanup for an attribute"""
    value = getattr(self, name)
    self._pre_cleanup(name, value)
    delattr(self, name)
    self._post_cleanup(name, value)

Bạn không thể làm điều đó với Del.del.

Đã trả lời ngày 15 tháng 10 năm 2013 lúc 11:49Oct 15, 2013 at 11:49

Hướng dẫn how do you drop an attribute in python? - làm thế nào để bạn thả một thuộc tính trong python?

1

Không nghi ngờ gì nữa là trước đây. Theo quan điểm của tôi, điều này giống như hỏi liệu

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
8 có tốt hơn
class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
9 không, và tôi không nghĩ ai đang hỏi câu hỏi đó :)

Hướng dẫn how do you drop an attribute in python? - làm thế nào để bạn thả một thuộc tính trong python?

Alex

469K197 Huy hiệu vàng868 Huy hiệu bạc977 Huy hiệu Đồng197 gold badges868 silver badges977 bronze badges

Đã trả lời ngày 13 tháng 7 năm 2009 lúc 17:55Jul 13, 2009 at 17:55

Alex Gaynoralex GaynorAlex Gaynor

13.9k9 Huy hiệu vàng61 Huy hiệu bạc111 Huy hiệu đồng9 gold badges61 silver badges111 bronze badges

5

Đó thực sự là một vấn đề ưu tiên, nhưng lần đầu tiên có lẽ là thích hợp hơn. Tôi chỉ sử dụng cái thứ hai nếu bạn không biết tên của thuộc tính mà bạn đang xóa trước thời hạn.

Đã trả lời ngày 13 tháng 7 năm 2009 lúc 17:34Jul 13, 2009 at 17:34

Jason Bakerjason BakerJason Baker

186K132 Huy hiệu vàng368 Huy hiệu bạc510 Huy hiệu Đồng132 gold badges368 silver badges510 bronze badges

Giống như GetAttr và SetAttr, Delattr chỉ nên được sử dụng khi không rõ tên thuộc tính.

Theo nghĩa đó, nó gần tương đương với một số tính năng python được sử dụng để truy cập chức năng tích hợp ở mức thấp hơn so với thông thường bạn có sẵn, chẳng hạn như

Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
0 thay vì
Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
1 và
Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
2 thay vì
Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
3

Đã trả lời ngày 13 tháng 7 năm 2009 lúc 18:00Jul 13, 2009 at 18:00

AlgoriasalgoriasAlgorias

2.9855 Huy hiệu vàng21 Huy hiệu bạc16 Huy hiệu đồng5 gold badges21 silver badges16 bronze badges

Không chắc chắn về các hoạt động bên trong, nhưng từ khả năng tái sử dụng mã và đừng là một quan điểm của đồng nghiệp giật, hãy sử dụng del. Nó rõ ràng và hiểu hơn bởi những người đến từ các ngôn ngữ khác.

Đã trả lời ngày 13 tháng 7 năm 2009 lúc 18:03Jul 13, 2009 at 18:03

Eleddyeleddyeleddy

1.2641 Huy hiệu vàng9 Huy hiệu bạc8 Huy hiệu đồng1 gold badge9 silver badges8 bronze badges

Đó là một câu hỏi cũ, nhưng tôi muốn đặt 2 xu của mình vào.

Mặc dù,

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
4 thanh lịch hơn, đôi khi bạn sẽ cần
class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y


A1 = A(2, 3)

print('Before deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')

#delete the attribute x
delattr(A1, 'x')
print('\nAfter deleting attribute from object A1')
result = hasattr(A1, 'x')
print(f'Does A1 has x? {result}')
result = hasattr(A1, 'y')
print(f'Does A1 has y? {result}')
5. Giả sử, nếu bạn có giao diện dòng lệnh tương tác cho phép người dùng xóa tự động bất kỳ thành viên nào trong đối tượng bằng cách nhập tên, thì bạn không có lựa chọn nào khác ngoài việc sử dụng biểu mẫu sau.

Đã trả lời ngày 8 tháng 7 năm 2014 lúc 11:23Jul 8, 2014 at 11:23

Kaosadkaosadkaosad

1.2031 Huy hiệu vàng11 Huy hiệu bạc15 Huy hiệu đồng1 gold badge11 silver badges15 bronze badges

Nếu bạn nghĩ

Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
6 rõ ràng hơn, thì tại sao không sử dụng
Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
7 mọi lúc thay vì
Before deleting attribute from object A1
Does A1 has x? True
Does A1 has y? True

After deleting attribute from object A1
Does A1 has x? False
Does A1 has y? True
8?

Đối với dưới mui xe ... dự đoán của bạn cũng tốt như của tôi. Nếu không tốt hơn đáng kể.

Đã trả lời ngày 13 tháng 7 năm 2009 lúc 17:59Jul 13, 2009 at 17:59

Dougal Matthewsdougal MatthewsDougal Matthews

Làm thế nào để bạn bỏ một biến trong Python?

Sử dụng lệnh 'del' là phương pháp được biết đến và dễ nhất để xóa các biến trong Python.Từ khóa Del xóa các đối tượng.Vì mọi thứ trong Python là một đối tượng, do đó, các danh sách, bộ dữ liệu và từ điển cũng có thể bị xóa bằng cách sử dụng 'del'. is the most known and easiest method to delete variables in Python. The del keyword deletes the objects. Since everything in Python is an object, therefore lists, tuples and dictionaries can also be deleted using 'del'.

Làm cách nào để xóa các thuộc tính?

Bấm đúp vào biểu tượng trong chế độ xem cây hoặc hình dạng trong sơ đồ đại diện cho phần tử. Bấm vào các thuộc tính, chọn thuộc tính bạn muốn xóa. Bấm xóa, sau đó bấm OK. Click Attributes, select the attribute you want to delete. Click Delete, and then click OK.

Làm thế nào để bạn xóa một mục khỏi danh sách trong Python?

Phương thức Remove () xóa phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách.Phương thức pop () loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa.Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

Làm thế nào để bạn loại bỏ một mục khỏi một lớp python?

Từ khóa Del trong Python chủ yếu được sử dụng để xóa các đối tượng trong Python. in python is primarily used to delete objects in Python.