Python内置函数(14)——delattr
英文文档:
delattr(object, name)- This is a relative of
setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example,delattr(x, 'foobar')is equivalent todel x.foobar. - 说明:
- 1. 函数作用用来删除指定对象的指定名称的属性,和setattr函数作用相反。
#定义类A
>>> class A:
def __init__(self,name):
self.name = name
def sayHello(self):
print('hello',self.name) #测试属性和方法
>>> a.name
'小麦'
>>> a.sayHello()
hello 小麦 #删除属性
>>> delattr(a,'name')
>>> a.name
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
a.name
AttributeError: 'A' object has no attribute 'name'
2. 当属性不存在的时候,会报错。
>>> a.name #属性name已经删掉,不存在
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
a.name
AttributeError: 'A' object has no attribute 'name' >>> delattr(a,'name') #再删除会报错
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
delattr(a,'name')
AttributeError: name
3. 不能删除对象的方法。
>>> a.sayHello
<bound method A.sayHello of <__main__.A object at 0x03F014B0>>
>>> delattr(a,'sayHello') #不能用于删除方法
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
delattr(a,'sayHello')
AttributeError: sayHello
>>>
Python内置函数(14)——delattr的更多相关文章
- Python内置函数(14)——bytes
英文文档: class bytes([source[, encoding[, errors]]]) Return a new "bytes" object, which is an ...
- python内置函数详细描述与实例演示
python有许多内置函数,列出表格如下 内置函数 abs() delattr() hash() memoryview() set() all() dict() help() min() setatt ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
随机推荐
- 咸鱼入门到放弃11--Servlet+JSP+JavaBean开发模式
本篇搬运了大佬blog:https://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servle ...
- 如何在Ubuntu 18.04中安装VMware Workstation Player
参考链接 如何在Ubuntu 18.04中安装VMware Workstation Player https://www.sysgeek.cn/ubuntu-18-04-install-vmware- ...
- 今日报错Cannot access java.lang.String
public java.long.Long getId() { return id; } public void setId(java.lang.Long id) { this.id = id;} 手 ...
- SOAPA来临,SIEM时代终结?
安全信息和事件管理(SIEM)产品及服务负责从大量企业安全控件.主机操作系统.企业应用和企业使用的其他软件中收集安全日志数据,并进行分析和报告.有些SIEM还可以试图阻止它们检测到正在进行的攻击,这可 ...
- 【C语言编程练习】5.10寻找水仙数
1. 题目要求 如果一个3位数等于各位数字的立方和,则称这个数为水仙数,例如407=4^3+0^3+7^3.编写一个程序,找出全部的水仙数 2. 题目分析 感觉又和之前的题目大同小异了,先找出解空间, ...
- wordcount源代码详解
package wordcount; import java.io.IOException; import java.util.StringTokenizer; import org.apache.h ...
- 在Linux上搭建测试环境常用命令(转自-测试小柚子)
一.搭建测试环境: 二.查看应用日志: (1)vivi/vim 原本是指修改文件,同时可以使用vi 日志文件名,打开日志文件(2)lessless命令是查看日志最常用的命令.用法:less 日志文件名 ...
- 批量删除Excel里面的换行符
关于批量删除excel里面的换行符,应该说写程序的遇上excel大体都会有这么个问题,在解决这个问题前,我的解决办法是把excel 的数据全部复制到txt里面, 因为操作txt比操作excel更为简单 ...
- Servlet 监听器Listner
定义: 专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动. Servlet 规范为每种事件监听器都定义了相应的接口,它用于监听 ...
- C# 窗体打开拖动到窗体的文件
private void Form3_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats ...