英文文档:

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 to del 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的更多相关文章

  1. Python内置函数(14)——bytes

    英文文档: class bytes([source[, encoding[, errors]]]) Return a new "bytes" object, which is an ...

  2. python内置函数详细描述与实例演示

    python有许多内置函数,列出表格如下 内置函数 abs() delattr() hash() memoryview() set() all() dict() help() min() setatt ...

  3. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  4. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  5. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  6. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  7. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  8. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  9. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

随机推荐

  1. eclipse 开发环境问题

    1.jdk安装,环境变量设置.主要有两个: JAVA_HOME   C:\Program Files\Java\jre7 JRE_HOME   C:\Program Files\Java\jre7 2 ...

  2. 3、Linux常用命令

    查看网络服务:netstat netstat -nltp 如果提示:命令找不到,需要安装net-tools包 sudo yum install net-tools 文件下载:wget wget -O ...

  3. html_jQuery_ajax

    ajax核心对象:  XMLHttpRequest 那年创建的XMLHttpRequest对象 创建的.. ajax 几种常用方法: load(); $.get(); $.post(); $.getS ...

  4. python案例——体脂率项目

    通过一个人的身高.体重.年龄.性别,判断这个人的体脂率,并且反馈是否正常? 首先接到项目后一定要一步步细分任务,直到每个细分的任务都可以用代码来实现. 写代码之前,先要确定python版本的问题 然后 ...

  5. C#实现 单点登录(SSO)

    SSO的基本概念 SSO英文全称Single Sign On(单点登录).SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同 ...

  6. [linux]CentOS安装pre-built Nginx

    官方文档:https://nginx.org/en/linux_packages.html Nginx安装分为软件包安装和pre-built安装.这里使用的pre-built安装,不用自己编译. 设置 ...

  7. 大数据 - Java基础:读取键盘输入的方法

    Java中获取键盘输入值的三种方法 程序编写中,从键盘获取数据是一件非常普通又平常的事 C:scanf() C++:cin() C#:Read().ReadKey().ReadLine() Java没 ...

  8. SpringBoot加Poi仿照EasyPoi实现Excel导出

    POI提供API给Java程序对Microsoft Office格式档案读和写的功能,详细功能可以直接查阅API,因为使用EasyPoi过程中总是缺少依赖,没有搞明白到底是什么坑,索性自己写一个简单工 ...

  9. 安装selenium

    步骤: 1.下载setuptools2.点击安装setuptools.exe3.安装成功后,python的安装目录下会有Scripts的文件夹4.配置环境变量:python安装目录\Scripts5. ...

  10. idea构建spring源码阅读环境

    注:由于文章不是一次性完成,下文中的test1目录和test目录应为同一个目录. (一)安装git和Gradle Spring项目托管在github之上,基于Gradle来构建项目.所以要想搭建Spr ...