浅析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 相关文章推荐
Python入门篇之函数
Oct 20 Python
在Python的Django框架下使用django-tagging的教程
May 30 Python
python 3利用BeautifulSoup抓取div标签的方法示例
May 28 Python
JPype实现在python中调用JAVA的实例
Jul 19 Python
基于numpy.random.randn()与rand()的区别详解
Apr 17 Python
DES加密解密算法之python实现版(图文并茂)
Dec 06 Python
pthon贪吃蛇游戏详细代码
Jan 27 Python
python下载微信公众号相关文章
Feb 26 Python
Python timer定时器两种常用方法解析
Jan 20 Python
离线状态下在jupyter notebook中使用plotly实例
Apr 24 Python
浅谈keras通过model.fit_generator训练模型(节省内存)
Jun 17 Python
python中导入 train_test_split提示错误的解决
Jun 19 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设计模式 Strategy(策略模式)
2011/06/26 PHP
ThinkPHP的MVC开发机制实例解析
2014/08/23 PHP
浅谈PHP中其他类型转化为Bool类型
2016/03/28 PHP
PHP CURL中传递cookie的方法步骤
2019/05/09 PHP
PHP从零开始打造自己的MVC框架之入口文件实现方法详解
2019/06/03 PHP
jQuery UI Autocomplete 体验分享
2012/02/14 Javascript
javascript中实现兼容JAVA的hashCode算法代码分享
2020/08/11 Javascript
使用JS实现jQuery的addClass, removeClass, hasClass函数功能
2014/10/31 Javascript
JS实现跟随鼠标闪烁转动色块的方法
2015/02/26 Javascript
Ionic实现仿通讯录点击滑动及$ionicscrolldelegate使用分析
2016/01/18 Javascript
JavaScript中的ParseInt(&quot;08&quot;)和“09”返回0的原因分析及解决办法
2016/05/19 Javascript
15个非常实用的JavaScript代码片段
2016/12/18 Javascript
js实现自定义路由
2017/02/04 Javascript
微信小程序实战之顶部导航栏(选项卡)(1)
2020/06/19 Javascript
JavaScript插入排序算法原理与实现方法示例
2018/08/06 Javascript
详解几十行代码实现一个vue的状态管理
2019/01/28 Javascript
微信小程序 动态修改页面数据及参数传递过程详解
2019/09/27 Javascript
openlayers4实现点动态扩散
2020/08/17 Javascript
python支持断点续传的多线程下载示例
2014/01/16 Python
python中pygame模块用法实例
2014/10/09 Python
Python字符串中查找子串小技巧
2015/04/10 Python
Python字符串格式化
2015/06/15 Python
Python实现列表转换成字典数据结构的方法
2016/03/11 Python
Python3爬楼梯算法示例
2019/03/04 Python
python实现LRU热点缓存及原理
2019/10/29 Python
python中删除某个元素的方法解析
2019/11/05 Python
django使用F方法更新一个对象多个对象字段的实现
2020/03/28 Python
python使用openpyxl操作excel的方法步骤
2020/05/28 Python
JACK & JONES英国官方网站:欧洲领先的男装生产商
2017/09/27 全球购物
印度尼西亚手表和包包商店:Urban Icon
2019/12/12 全球购物
俄罗斯第一家多品牌在线奢侈品精品店:Aizel.ru
2020/09/06 全球购物
行政助理的岗位职责
2014/02/18 职场文书
全国爱牙日活动总结
2015/02/05 职场文书
世界水日宣传活动总结
2015/02/09 职场文书
论文答辩开场白大全
2015/05/27 职场文书
合作意向书怎么写
2019/06/24 职场文书