python 内建函数setattr() getattr()
python 内建函数setattr() getattr()
setattr(object,name,value):
作用:设置object的名称为name(type:string)的属性的属性值为value,属性name可以是已存在属性也可以是新属性。
getattr(object,name,default):
作用:返回object的名称为name的属性的属性值,如果属性name存在,则直接返回其属性值;如果属性name不存在,则触发AttribetError异常或当可选参数default定义时返回default值。
<!-- lang: python -->
>>> class attribute():
... def __init__(self):
... self.attribute_1='i am attribute 1'
... self.attribute_2='i am attribute 2'
...
>>> result=attribute()
>>> dir(result)
['__doc__', '__init__', '__module__', 'attribute_1', 'attribute_2']
>>> getattr(result,'attribute_1')
'i am attribute 1'
>>> getattr(result,'attribute_3')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: attribute instance has no attribute 'attribute_3'
>>> getattr(result,'attribute_3','i am attribute 3')
'i am attribute 3'
>>> dir(result)
['__doc__', '__init__', '__module__', 'attribute_1', 'attribute_2']
>>> getattr(result,'attribute_2')
'i am attribute 2'
>>> setattr(result,'attribute_2','i am changed')
>>> getattr(result,'attribute_2')
'i am changed'
>>> setattr(result,'attribute_4','i am new one')
>>> dir(result)
['__doc__', '__init__', '__module__', 'attribute_1', 'attribute_2', 'attribute_4']
python 内建函数setattr() getattr()的更多相关文章
- python之setattr,getattr,hasattr
可以使用setattr(), getattr(), hasattr()动态对实例进行操作. 相当于Java中的反射机制, 或者更确切地, 像JavaScript中属性操作. 具体属性: __dict_ ...
- 【转】Python的hasattr() getattr() setattr() 函数使用方法详解
Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...
- python自省函数getattr的用法
getattr是python里的一个内建函数 getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例.这样,你就可以把一个类可能要调用的方法放在配置文件里,在需要的时 ...
- python 内建函数 filter,map和reduce
python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用. ...
- Python内建函数-callable
Python内建函数-callable callable(object) 中文说明:检查对象object是否可调用.如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojb ...
- Python内建函数enumerate()用法及在for循环应用
Python 内建函数enumerate() 由于这个单纯很长,不容易记住,用法还是比较广泛的,下面讲述Python内建函数enumerate()用法. 1,实例 enumerate(sequence ...
- Python内建函数reduce()用法
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,下面讲述Python内建函数reduce()用法. ...
- python eval() hasattr() getattr() setattr() 函数使用方法详解
eval() 函数 --- 将字符串str当成有效的表达式来求值并返回计算结果. 语法:eval(source[, globals[, locals]]) ---> value 参数: sour ...
- Python的hasattr() getattr() setattr() 函数使用方法详解
hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来 1 ...
随机推荐
- Bitmap二次采样(处理图片过大的问题)
private Bitmap createImageThumbnail(String filePath, int newHeight, int newWidth) { BitmapFactory.Op ...
- [转]jquery 点击表格变为input可以修改无刷新更新数据
原文地址:http://www.freejs.net/article_biaodan_43.html 之前已经发了2篇类似的文章<点击变td为input更新>和<jquery表格可编 ...
- leetcode(一)Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- URI 中特殊字符处理
一.问题阐述 今天写 url 请求时,不管是get 请求还是 post 请求,如果参数中带有 + % # 等特殊符号,就无法正常获得参数 具体现象就是 用URL传参数的时候,用&符号连接,如果 ...
- Echarts-柱状图柱图宽度设置
先看两张图 图中柱图只需要设置series中的坐标系属性barWidth就可以, 这种图柱状图,折叠柱状图都适应 eg: /** * 堆积柱状图 * @param xaxisdata x轴:标签(数组 ...
- eclipse运行一个类却运行的是另外一个类,报无法加载的类
今天是用eclipse想运行一个a.java但是却加载是b.java文件,而且还报错:无法加载的主类 查了半天才发现a.java文件中:public static void main(){}这里的ma ...
- 【奶昔队ROUND#1】
奶昔队Round #1 热身 (奶昔队不是真正的队,是群) CodeForces 435C Cardiogram 模拟,不过我做的时候不是模拟,是计算...(写了好久,还wa了几次),现在看了别人的代 ...
- ffmpeg 转成MP3采样率8000
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- BZOJ 1064 假面舞会(NOI2008) DFS判环
此题,回想Sunshinezff学长给我们出的模拟题,原题啊有木有!!此处吐槽Sunshinezff爷出题不人道!! 不过也感谢Sunshinezff学长的帮助,我才能做出来.. 1064: [Noi ...
- VMware“该虚拟机似乎正在使用中”问题
在用VMware虚拟机的时候,有时会发现打开虚拟机时提示“该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权.否则,请按“取消(C)”按钮以防损坏.配置文件: D ...