📜  Python中的 delattr() 和 del()

📅  最后修改于: 2022-05-13 01:55:47.257000             🧑  作者: Mango

Python中的 delattr() 和 del()

德拉特

delattr()方法用于从对象中删除命名属性,需要对象的事先许可。
句法:

delattr(object, name)

The function takes only two parameter:

object :  from which 
the name attribute is to be removed.
name :  of the attribute 
which is to be removed.

The function doesn't returns any value, 
it just removes the attribute, 
only if the object allows it.

工作:假设我们有一个名为 Geek 的类,它有五个学生作为属性。因此,使用 delattr() 方法,我们可以删除任何一个属性。

Python3
# Python code to illustrate delattr()
class Geek:
  stu1 = "Henry"
  stu2 = "Zack"
  stu3 = "Stephen"
  stu4 = "Amy"
  stu5 = "Shawn"
 
names = Geek()
 
print('Students before delattr()--')
print('First = ',names.stu1)
print('Second = ',names.stu2)
print('Third = ',names.stu3)
print('Fourth = ',names.stu4)
print('Fifth = ',names.stu5)
 
# implementing the method
delattr(Geek, 'stu5')
 
print('After deleting fifth student--')
print('First = ',names.stu1)
print('Second = ',names.stu2)
print('Third = ',names.stu3)
print('Fourth = ',names.stu4)
# this statement raises an error
print('Fifth = ',names.stu5)


Python3
# Python code to illustrate del()
class Geek:
  stu1 = "Henry"
  stu2 = "Zack"
  stu3 = "Stephen"
  stu4 = "Amy"
  stu5 = "Shawn"
 
names = Geek()
 
print('Students before del--')
print('First = ',names.stu1)
print('Second = ',names.stu2)
print('Third = ',names.stu3)
print('Fourth = ',names.stu4)
print('Fifth = ',names.stu5)
 
# implementing the operator
del Geek.stu5
 
print('After deleting fifth student--')
print('First = ',names.stu1)
print('Second = ',names.stu2)
print('Third = ',names.stu3)
print('Fourth = ',names.stu4)
# this statement raises an error
print('Fifth = ',names.stu5)


输出:

Students before delattr()--
First =  Henry
Second =  Zack
Third =  Stephen
Fourth =  Amy
Fifth =  Shawn
After deleting fifth student--
First =  Henry
Second =  Zack
Third =  Stephen
Fourth =  Amy

当执行移到程序的最后一行时,即调用第五个属性时,编译器会报错:

Traceback (most recent call last):
  File "/home/028e8526d603bccb30e9aeb7ece9e1eb.py", line 25, in 
    print('Fifth = ',names.stu5)
AttributeError: 'Geek' object has no attribute 'stu5'

删除运算符

Python中有另一个运算符与 delattr() 方法执行类似的工作。它是del运算符。让我们看看它是如何工作的。

Python3

# Python code to illustrate del()
class Geek:
  stu1 = "Henry"
  stu2 = "Zack"
  stu3 = "Stephen"
  stu4 = "Amy"
  stu5 = "Shawn"
 
names = Geek()
 
print('Students before del--')
print('First = ',names.stu1)
print('Second = ',names.stu2)
print('Third = ',names.stu3)
print('Fourth = ',names.stu4)
print('Fifth = ',names.stu5)
 
# implementing the operator
del Geek.stu5
 
print('After deleting fifth student--')
print('First = ',names.stu1)
print('Second = ',names.stu2)
print('Third = ',names.stu3)
print('Fourth = ',names.stu4)
# this statement raises an error
print('Fifth = ',names.stu5)

输出:

Students before del--
First =  Henry
Second =  Zack
Third =  Stephen
Fourth =  Amy
Fifth =  Shawn
After deleting fifth student--
First =  Henry
Second =  Zack
Third =  Stephen
Fourth =  Amy

结果与错误相同:

Traceback (most recent call last):
  File "/home/7c239eef9b897e964108c701f1f94c8a.py", line 26, in 
    print('Fifth = ',names.stu5)
AttributeError: 'Geek' object has no attribute 'stu5'

del 与delattr()

  1. 动态删除: del 更加明确和高效,delattr() 允许动态删除属性。
  2. 速度:如果考虑并运行上述程序,则执行速度会略有不同。 del 与 delattr() 相比稍,具体取决于机器。
  3. 字节码指令:与 delattr() 相比,del 也需要更少的字节码指令。

所以我们总结比较说 del 比 delattr 稍微快一点,但是当涉及到属性的动态删除时, delattr() 具有优势,因为 del运算符是不可能的。