浅析Python中的getattr(),setattr(),delattr(),hasattr()


Posted in Python onJune 14, 2016

getattr()函数是Python自省的核心函数,具体使用大体如下:

获取对象引用getattr

Getattr用于返回一个对象属性,或者方法

class A: 
def __init__(self): 
self.name = 'zhangjing' 
 #self.age=''
def method(self): 
print"method print" 
Instance = A() 
print getattr(Instance , 'name, 'not find') #如果Instance 对象中有属性name则打印self.name的值,否则打印'not find'
print getattr(Instance , 'age', 'not find') #如果Instance 对象中有属性age则打印self.age的值,否则打印'not find'
print getattr(a, 'method', 'default') 
#如果有方法method,否则打印其地址,否则打印default 
print getattr(a, 'method', 'default')() 
#如果有方法method,运行函数并打印None否则打印default

注:使用getattr可以轻松实现工厂模式。

例:一个模块支持html、text、xml等格式的打印,根据传入的formate参数的不同,调用不同的函数实现几种格式的输出

import statsout 
def output(data, format="text"): 
output_function = getattr(statsout, "output_%s" % format) 
return output_function(data) 
setattr(object, name, value)
This is the counterpart of getattr(). The arguments
are an object, a string and an arbitrary value. The string may name an existing
attribute or a new attribute. The function assigns the value to the attribute,
provided the object allows it. For example, setattr(x,
'foobar', 123) is equivalent to
x.foobar = 123.

这是相对应的getattr()。参数是一个对象,一个字符串和一个任意值。字符串可能会列出一个现有的属性或一个新的属性。这个函数将值赋给属性的。该对象允许它提供。例如,setattr(x,“foobar”,123)相当于x.foobar = 123。

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.

与setattr()相关的一组函数。参数是由一个对象(记住python中一切皆是对象)和一个字符串组成的。string参数必须是对象属性名之一。该函数删除该obj的一个由string指定的属性。delattr(x, 'foobar')=del x.foobar

•hasattr用于确定一个对象是否具有某个属性。

语法:

hasattr(object, name) -> bool

判断object中是否有name属性,返回一个布尔值。

>>> li=["zhangjing","zhangwei"]
>>> getattr(li,"pop")
<built-in method pop of list object at 0x011DF6C0>
>>> li.pop
<built-in method pop of list object at 0x011DF6C0>
>>> li.pop()
'zhangwei'
>>> getattr(li,"pop")()
'zhangjing'
>>>getattr(li, "append")("Moe")
Python 相关文章推荐
解决Pycharm无法import自己安装的第三方module问题
May 18 Python
Python中一些深不见底的“坑”
Jun 12 Python
Python实现时间序列可视化的方法
Aug 06 Python
django重新生成数据库中的某张表方法
Aug 28 Python
Python使用gluon/mxnet模块实现的mnist手写数字识别功能完整示例
Dec 18 Python
python装饰器的特性原理详解
Dec 25 Python
tensorflow实现训练变量checkpoint的保存与读取
Feb 10 Python
Anaconda3+tensorflow2.0.0+PyCharm安装与环境搭建(图文)
Feb 18 Python
python实现xlwt xlrd 指定条件给excel行添加颜色
Jul 14 Python
Python pathlib模块使用方法及实例解析
Oct 05 Python
python 模拟登陆163邮箱
Dec 15 Python
Python基础之数据结构详解
Apr 28 Python
Python中getattr函数和hasattr函数作用详解
Jun 14 #Python
Python模块包中__init__.py文件功能分析
Jun 14 #Python
Python计算字符宽度的方法
Jun 14 #Python
Python中文分词实现方法(安装pymmseg)
Jun 14 #Python
Python找出list中最常出现元素的方法
Jun 14 #Python
Python中列表元素转为数字的方法分析
Jun 14 #Python
python实现中文转换url编码的方法
Jun 14 #Python
You might like
php循环创建目录示例分享(php创建多级目录)
2014/03/04 PHP
Yii框架中jquery表单验证插件用法示例
2016/10/18 PHP
php使用lua+redis实现限流,计数器模式,令牌桶模式
2019/04/04 PHP
限制文本框输入N个字符的js代码
2010/05/13 Javascript
Table冻结表头示例代码
2013/08/20 Javascript
基于jquery异步传输json数据格式实例代码
2013/11/23 Javascript
基于zepto.js实现仿手机QQ空间的大图查看组件ImageView.js详解
2015/03/05 Javascript
js时间比较 js计算时间差的简单实现方法
2016/08/26 Javascript
在js里怎么实现Xcode里的callFuncN方法(详解)
2016/11/05 Javascript
微信小程序 radio单选框组件详解及实例代码
2017/01/10 Javascript
jQuery EasyUI ProgressBar进度条组件
2017/02/28 Javascript
JavaScript中最常用的10种代码简写技巧总结
2017/06/28 Javascript
vue-resource请求实现http登录拦截或者路由拦截的方法
2018/07/11 Javascript
Electron + vue 打包桌面操作流程详解
2019/06/24 Javascript
[00:53]TI3正赛第三天 DK怒破A队不败金身 现场国旗飘扬热血激昂
2013/08/10 DOTA
[01:38]DOTA2辉夜杯 欢乐的观众现场采访
2015/12/26 DOTA
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
2015/05/20 Python
浅谈python类属性的访问、设置和删除方法
2016/07/25 Python
Python正则抓取网易新闻的方法示例
2017/04/21 Python
Python 反转字符串(reverse)的方法小结
2018/02/20 Python
解决Pycharm无法import自己安装的第三方module问题
2018/05/18 Python
使用wxpy实现自动发送微信消息功能
2020/02/28 Python
CSS3 @media的基本用法总结
2019/09/10 HTML / CSS
Haglöfs瑞典官方网站:haglofs火柴棍,欧洲顶级户外品牌
2018/10/18 全球购物
美国在线眼镜店:GlassesShop
2018/11/15 全球购物
美国糖果店:Sugarfina
2019/02/21 全球购物
应届生护士求职信
2013/11/01 职场文书
家长写给孩子的评语
2014/04/18 职场文书
自我检讨书范文
2015/01/28 职场文书
2015年感恩节演讲稿(优选篇)
2015/03/20 职场文书
2019毕业典礼主持词!
2019/07/05 职场文书
诺贝尔奖获得者名言100句:句句启人心智,值永久收藏
2019/08/09 职场文书
有关花店创业的计划书模板
2019/08/27 职场文书
python urllib库的使用详解
2021/04/13 Python
Python爬虫基础之爬虫的分类知识总结
2021/05/13 Python
Win11快速关闭所有广告推荐
2022/04/19 数码科技