[转]Python的getattr(),setattr(),delattr(),hasattr()
getattr()函数是Python自省的核心函数,具体使用大体如下:
获取对象引用getattr
Getattr用于返回一个对象属性,或者方法
- class A:
- def __init__(self):
- self.name = 'zhangjing'
- #self.age='24'
- 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, is equivalent to
'foobar', 123)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的getattr(),setattr(),delattr(),hasattr()的更多相关文章
- Python的getattr(),setattr(),delattr(),hasattr()及类内建__getattr__应用
@Python的getattr(),setattr(),delattr(),hasattr() 先转一篇博文,参考.最后再给出一个例子 getattr()函数是Python自省的核心函数,具体使用大体 ...
- Python的getattr(),setattr(),delattr(),hasattr()
判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来 1 >>> class ...
- python 内置函数的补充 isinstance,issubclass, hasattr ,getattr, setattr, delattr,str,del 用法,以及元类
isinstance 是 python中的内置函数 , isinstance()用来判断一个函数是不是一个类型 issubclass 是python 中的内置函数, 用来一个类A是不是另外一个 ...
- isinstance/type/issubclass的用法,反射(hasattr,getattr,setattr,delattr)
6.23 自我总结 面向对象的高阶 1.isinstance/type/issubclass 1.type 显示对象的类,但是不会显示他的父类 2.isinstance 会显示的对象的类,也会去找对象 ...
- Python hasattr,getattr,setattr,delattr
#!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-11-04 # 反 ...
- python动态函数hasattr,getattr,setattr,delattr
hasattr(object,name) hasattr用来判断对象中是否有name属性或者name方法,如果有,染回true,否则返回false class attr(): def fun( ...
- python反射hasattr getattr setattr delattr
反射 : 是用字符串类型的名字 去操作 变量 相比于用eval('print(name)') 留有 安全隐患 反射 就没有安全问题 hasattr 语法: hasattr(object, name)o ...
- hasattr getattr setattr delattr --> (反射)
class Room: def __init__(self,name): self.name = name def big_room(self): print('bigroot') R = Room( ...
- Python笔记_第四篇_高阶编程_反射_(getattr,setattr,deattr,hasattr)
1. 元数据和反射概念: 有关程序及其类型的数据成为元数据(metadata),他保存在程序的程序集中. 反射这个词儿听起来比较陌生.程序在运行时,可以查看其它程序集或其本身的元数据.一个运行的程序查 ...
随机推荐
- Suricata规则配置
Suricata 规则配置 IDS/IPS/WAF IPS.IDS和WAF分别是入侵防御系统和入侵检测系统以及WEB应用防火墙的简称,很多人说这些玩意不就是盒子吗已经过时了,其实不是,SIEM其实是有 ...
- Windows Phone 几种弹出框提示方式
首先,我们需要在网络上下载一个Coding4Fun 然后,引用 using Coding4Fun.Phone.Controls.Toolkit; using Codin ...
- SMGP3.0协议的概念知识
该项目主页在https://code.google.com/archive/p/smgp/,可以使用VPN进去看看,该项目是开源的,根据SMGP3.0协议写的API,我们要用的话直接调用就好了,这里主 ...
- Linux 搭建Nginx并添加配置 SSL 证书
1. 安装准备 1.1 gcc安装 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: [root@nginx ~]# yum -y i ...
- EasyUI之Layout布局和Tabs页签的使用
1.JQuery EasyUI之LayOut布局 EasyUI是一款基于JQuery开发的前端框架,它集成很多漂亮的样式和相应的功能,大大方便了我们对前端开发的难度.对于web项目而言,主页面的一定是 ...
- Google Now 'not available in your country'
Google Now 'not available in your country' Don't know how to cope with this problem.
- Nginx文件下载服务器
1. 配置文件 server { listen 80; #端口 server_name localhost; #服务名 charset utf-8; #避免中文乱码 root /data/packag ...
- [.Net]System.OutOfMemoryException异常
1. 一个异常情景 加载15000条等高线,平均每条线有400个点到三维球上,等待时间太长.而且可能会报内存异常. 2. 不错的分析 http://wenku.baidu.com/view/14471 ...
- 岭回归&Lasso回归
转自:https://blog.csdn.net/dang_boy/article/details/78504258 https://www.cnblogs.com/Belter/p/8536939. ...
- 【转】Deep Learning(深度学习)学习笔记整理系列之(五)
9.2.Sparse Coding稀疏编码 如果我们把输出必须和输入相等的限制放松,同时利用线性代数中基的概念,即O = a1*Φ1 + a2*Φ2+….+ an*Φn, Φi是基,ai是系数,我们可 ...