@Python的getattr(),setattr(),delattr(),hasattr()

先转一篇博文,参考。最后再给出一个例子

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

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

  1. class A:
  2. def __init__(self):
  3. self.name = 'zhangjing'
  4.     #self.age='24'
  5. def method(self):
  6. print"method print"
  7. Instance = A()
  8. print getattr(Instance , 'name, 'not find') #如果Instance 对象中有属性name则打印self.name的值,否则打印'not find'
  9. print getattr(Instance , 'age', 'not find')   #如果Instance 对象中有属性age则打印self.age的值,否则打印'not find'
  10. print getattr(a, 'method', 'default')
  11. #如果有方法method,否则打印其地址,否则打印default
  12. print getattr(a, 'method', 'default')()
  13. #如果有方法method,运行函数并打印None否则打印default

注:使用getattr可以轻松实现工厂模式。
例:一个模块支持html、text、xml等格式的打印,根据传入的formate参数的不同,调用不同的函数实现几种格式的输出

    1. import statsout
    2. def output(data, format="text"):
    3. output_function = getattr(statsout, "output_%s" % format)
    4. 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") 

--------------------------------------------------------------------------------------------------------------------------------------------------------

例子如下:

 
 class WrapMe(object):
"""docstring for ClassName"""
def __init__(self, arg): self.__data = arg
def get(self):
return self.__data
def __repr__(self):
return 'self.__data'
def __str__(self):
return str(self.__data)
def __getattr__(self,attr):
return getattr(self.__data,attr)
wrappedComplex = WrapMe(3.5+4.2j)
print wrappedComplex
print wrappedComplex.real
print wrappedComplex.imag
print wrappedComplex.conjugate()
print wrappedComplex.get()

结果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALoAAACACAIAAAAzjeRtAAAAA3NCSVQICAjb4U/gAAAQqklEQVR4Xu1dCVgT1xaerCyCK0TBBRdQfK1P3KrVutS6gguIKIpWsOJXbC3WBVxfy1NxqaLiblHxWbUUqFiKVitVaq0oosGNsC8FZDOsGiAheZOEQCBzJxmSCWG48/H54b3nnnvOf/65987M4V5aQUEBAi+IgGYI0DUTg1IQASkCtJEjR0IkIAIaIsB0cnLSUBSKQQSYmZmZEAWIgIYIwLWLhkBBMSkCkC6QBwQQoA2xH0hAHIp2bATg6NKx40/Qexy6dBq1Leaa71AjghoNRJxhvTT8UcSKvkwle9h2ayJuBIwzNxAT26EZQLowes9dN70o9MeUWrlXdIspfmeu3UlI4nKT4q+f2zJngDG+u2aTTz+RCjf83N83xgS/QUMte6DX5adJ3J+WWjPw5BmWE1btvhBz7wmq/+/o0xum92Y3ExcLMu/G3E1/K1YqrcsID82b/JVzMw7hdQLrWiCgfPMpV7EGLfAYxDsaV6yAm0aXlCb+uO9iRmElYj358x27zkhy5+19JsBDVFQZu3ttSKqUcBJheU4NnmxDHXvg0h0LadkNHMVpwO43eazp8yt7z/IK6m0c12/97kz9kgWHkxsbSsrijwfEt1AgLvkr7KX/+oWDwg6l1OEoh1UABAB0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CANiuncZwnbMJ/iSDU+lC6QgoaMAc+/HFn2khnM9Ou5QmruMfkSdPiFdU6aaHl/1YNFv18ggmbWAt8/1AGHEG0kyxZBB52eSXq8ehpgDcbjzBtHcJqC0lI4CiMpoa4FGkDqgNjq2v7wm6nqCTmpbHEm1FCKruE9amHMh3dSxzdZ3lRLT7Btb6q5Oq3bNfB1z0lv11Xtca8GKtru1WDtn6+BEG3zCTgOzK9d/PLH4MNxe/zkKV+Q2vILaCDc3RUOO7u+WGlEHHCA5bTJj73+Nt7wC6iwTjFbA2u00SHgnDTSIXr0FBzXdXDfxu+6c2U69sBLKdmi14bFaSAet6UOyHCsvjrqxkFUFJfFKaQKfKIGYvttdHoJW3ftgMzv2NZdtBNX6J1YH9k2yhVanDZnbPsHzved9FLncoYGdmiDtRIGx7pJqOG6zuqqBS5HPu5SyhmTrDxfRAMh2ujOC3LM+X9Is5CbyzC4rsMu0t9/dwo3WR94KeVFFFPFOEED4Q4I7Zy5y/j/lNROmv/z19BQrP8md4KEleX7aKCek77PWr7+THZx2+uLXCUsVgMVbcUuXYGCV7QVuGjFilzRVuzSFSh4RVuBi1asyBVtxS5dgYJXtBW4aMWKXNFW7NIVKHhFW4GLVqzIFW3FLl2Bgle0FbhoxYpc0Vbs0hUoeEVbgYtWrMgVbcUuXYGCV7QVuGjFivwf0Chrf8yUIK8AAAAASUVORK5CYII=" alt="" />

----------------------------------------------------------

类内建是对象的获取,内建是获取XX中的XXX方法结果

Python的getattr(),setattr(),delattr(),hasattr()及类内建__getattr__应用的更多相关文章

  1. [转]Python的getattr(),setattr(),delattr(),hasattr()

    getattr()函数是Python自省的核心函数,具体使用大体如下: 获取对象引用getattrGetattr用于返回一个对象属性,或者方法 class A: def __init__(self): ...

  2. Python的getattr(),setattr(),delattr(),hasattr()

    判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来   1 >>> class ...

  3. python 内置函数的补充 isinstance,issubclass, hasattr ,getattr, setattr, delattr,str,del 用法,以及元类

    isinstance   是 python中的内置函数 , isinstance()用来判断一个函数是不是一个类型 issubclass  是python 中的内置函数,  用来一个类A是不是另外一个 ...

  4. isinstance/type/issubclass的用法,反射(hasattr,getattr,setattr,delattr)

    6.23 自我总结 面向对象的高阶 1.isinstance/type/issubclass 1.type 显示对象的类,但是不会显示他的父类 2.isinstance 会显示的对象的类,也会去找对象 ...

  5. python进阶之函数和类内建魔法属性

    前言 关于对象的魔法方法我们已经讲得太多,但是对于类或函数内建的魔法属性和功能我们涉及较少,下面系统了解一下类和函数的内建属性. 查看内建属性 class Person(object): pass d ...

  6. Python的程序结构[5] -> 模块/Module[0] -> 内建模块 builtins

    builtins 内建模块 / builtins Module 在Python的模块中,有一种特殊模块,无需导入便可以使用,其中包含了许多内建函数与类. builtins 模块内容 / builtin ...

  7. Python hasattr,getattr,setattr,delattr

    #!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-11-04 # 反 ...

  8. python反射hasattr getattr setattr delattr

    反射 : 是用字符串类型的名字 去操作 变量 相比于用eval('print(name)') 留有 安全隐患 反射 就没有安全问题 hasattr 语法: hasattr(object, name)o ...

  9. python动态函数hasattr,getattr,setattr,delattr

    hasattr(object,name) hasattr用来判断对象中是否有name属性或者name方法,如果有,染回true,否则返回false class attr():     def fun( ...

随机推荐

  1. photoshop cc 2014 下载安装及汉化资源及切图简要使用教程

    这是百度经验上一个pscc 2014 版本的下载安装汉化教程,亲测有效: http://jingyan.baidu.com/article/647f0115bce3847f2148a80c.html ...

  2. vscode下eslint代码规范

    直接上规范吧: // 将设置放入此文件中以覆盖默认设置 { "editor.fontSize": 17, "editor.tabSize": 2, " ...

  3. java语言的各种输入情况(ACM常用)

    1.只输入一组数据: Scanner s=new Scanner(System.in); int a=s.nextInt(); int b=s.nextInt(); 2.输入有多组数据,没有说明输入几 ...

  4. 关于二进制和字符串及base64格式

    字符串转二进制可以直接转,而二进制转字符串不可以,其中间包含的各种特殊符号,转成字符串时会出现问题,需要将二进制进行base64编码,并且需要在结尾加上#0表示结尾,然后转成字符串.

  5. Oracle数据库基本操作 (六) —— 数据的导出与导入

    一.cmd 下登录oracle数据库下的指定用户 方式一:命令行明文连接登录 打开cmd,输入:sqlplus/nolog 输入:conn username/passworld@数据库实例名 方式二: ...

  6. 《JavaWeb从入门到改行》fileupload,没毛病

    目录: »  fileupload API >  文件上传的要求 >  fileupload组件 »  上传细节的代码演示 »  项目案例-上传头像并显示 fileupload API 文 ...

  7. Hibernate 注解(Annotations 四)多对多双向注解

    注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量.方法参数等的前面,用来 ...

  8. js中contains()方法的了解

    今天第一次碰到了contains()方法,处于好奇了解了一下:发现在某些场合还是挺有用的. contains(),js原生方法,用于判断DOM元素的包含关系: 需要注意的是:它以HTMLElement ...

  9. 1083 Cantor表

    题目描述 Description 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/ ...

  10. GIS 地理坐标分类

    wgs84 GPS系统直接通过卫星定位获得的坐标.(最基础的坐标.) gcj02 兲朝已安全原因为由,要求在中国使用的地图产品使用的都必须是加密后的坐标.这套加密后的坐标就是gcj02 google的 ...