delattr()函数将一个对象作为参数,并从该对象中移除一个属性。

创新互联是一家集网站建设,增城企业网站建设,增城品牌网站建设,网站定制,增城网站建设报价,网络营销,网络优化,增城网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
 **delattr(object, name)** #where name is the name of attribute 
delattr()参数:该函数将一个对象作为参数,要删除的属性可以作为可选参数传递。
| 参数 | 描述 | 必需/可选 | 
|---|---|---|
| 一个对象 | 要从中删除属性的对象 | 需要 | 
| 属性 | 要移除的属性的名称 | 可选择的 | 
delattr()函数不返回任何内容。它只从对象中移除属性
delattr()方法的示例 class Student:
name = "Ram" age = 22
course = "Bachelors"
Student1 = Student()
delattr(Student, 'age') #Removing age attribute
print("Student's name is ",Student 1.name) 
print("Student's country is ",Student 1.country)
print("Student's age is ",Student 1.age) # Raises error since age is not found 
输出:
Student's name is John 
Student's country is Norway
AttributeError: 'Student' object has no attribute 'age' 
delattr()是如何工作的? class Coordinate:
  x = 20
  y = -15
  z = 0
value1 = Coordinate() 
print('x = ',value1.x)
print('y = ',value1.y)
print('z = ',value1.z)
delattr(Coordinate, 'z')
print('--After deleting z attribute--')
print('x = ',value1.x)
print('y = ',value1.y)
# Raises Error
print('z = ',value1.z) 
输出:
x =  20
y =  -15
z =  0
--After deleting z attribute--
x =  20
y =  -15
Traceback (most recent call last):
  File "python", line 19, in AttributeError: 'Coordinate' object has no attribute 'z'              Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号