反射之hasattr() getattr() setattr() 函数

Python的hasattr() getattr() setattr() 函数使用方法详解

hasattr(object, name)
判断object中有没有一个name字符串对应的方法或属性,返回BOOL值,有name特性返回True, 否则返回False。
需要注意的是name要用括号括起来

1 >>> class test():
2 ... name="xiaohua"
3 ... def run(self):
4 ... return "HelloWord"
5 ...
6 >>> t=test()
7 >>> hasattr(t, "name") #判断对象有name属性
8 True
9 >>> hasattr(t, "run") #判断对象有run方法
10 True
11 >>>

def getattr(object, name, default=None): # known special case of getattr
"""
getattr(object, name[, default]) -> value

Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
"""
pass
获取对象object的属性或者方法,如果存在打印出来,如果不存在,打印出默认值,默认值可选。
需要注意的是,如果是返回的对象的方法,返回的是方法的内存地址,如果需要运行这个方法,
可以在后面添加一对括号。

1 >>> class test():
2 ... name="xiaohua"
3 ... def run(self):
4 ... return "HelloWord"
5 ...
6 >>> t=test()
7 >>> getattr(t, "name") #获取name属性,存在就打印出来。
8 'xiaohua'
9 >>> getattr(t, "run") #获取run方法,存在就打印出方法的内存地址。
10 <bound method test.run of <__main__.test instance at 0x0269C878>>
11 >>> getattr(t, "run")() #获取run方法,后面加括号可以将这个方法运行。
12 'HelloWord'
13 >>> getattr(t, "age") #获取一个不存在的属性,没有默认值,会报错。
14 Traceback (most recent call last):
15 File "<stdin>", line 1, in <module>
16 AttributeError: test instance has no attribute 'age'
17 >>> getattr(t, "age",'不存在啊') #若属性不存在,设置默认值,返回一个默认值。
18 不存在啊
19 >>>

setattr(object, name, values)

给对象的属性赋值,若属性不存在,先创建再赋值。

1 >>> class test():
2 ... name="xiaohua"
3 ... def run(self):
4 ... return "HelloWord"
5 ...
6 >>> t=test()
7 >>> hasattr(t, "age") #判断属性是否存在
8 False
9 >>> setattr(t, "age", "18") #为属相赋值,并没有返回值
10 >>> hasattr(t, "age") #属性存在了
11 True
12 >>>

类也是对象,也可以调用反射方法
class Foo(object):

staticField = "old boy"

def __init__(self):
self.name = 'wupeiqi'

def func(self):
return 'func'

@staticmethod
def bar():
return 'bar'

print getattr(Foo, 'staticField')
print getattr(Foo, 'func')
print getattr(Foo, 'bar')

一种综合的用法是:判断一个对象的属性是否存在,若不存在就添加该属性。

1 >>> class test():
2 ... name="xiaohua"
3 ... def run(self):
4 ... return "HelloWord"
5 ...
6 >>> t=test()
7 >>> getattr(t, "age") #age属性不存在
8 Traceback (most recent call last):
9 File "<stdin>", line 1, in <module>
10 AttributeError: test instance has no attribute 'age'
11 >>> getattr(t, "age", setattr(t, "age", "18")) #age属性不存在时,设置该属性
12 '18'
13 >>> getattr(t, "age") #可检测设置成功
14 '18'
15 >>>
好处之一:
两个A,B程序员协调开发,A在写程序的时候需要用到B所写的类,但是B的类还没有实现,使用了反射机制A可以继续完成自己的代码,
总之反射的好处就是,可以事先定义好接口,接口只有在被完成后才会真正执行,这实现了即插即用,这其实是一种‘后期绑定’,
什么意思?即你可以事先把主要的逻辑写好(只定义接口),然后后期再去实现接口的功能
B程序员:
class FtpClient:
'ftp客户端,但是还么有实现具体的功能'
def __init__(self,addr):
print('正在连接服务器[%s]' %addr)
self.addr=addr
A程序员:
#from module import FtpClient
f1=FtpClient('192.168.1.1')
if hasattr(f1,'get'):
func_get=getattr(f1,'get')
func_get()
else:
print('---->不存在此方法')
print('处理其他的逻辑')

好处之二:
动态导入模块(基于反射当前模块成员),如果反射返回的模块是字符串,但是需要导入“FtpClient”,就不能用from...import...
1. import importlib
importlib.import_module(“FtpClient”) 这样就可以正常导入了,官方推荐方法
2. __import__.(“FtpClient”) #解释器内部使用
两种方法效果相同

原文地址:https://www.cnblogs.com/jinxf/p/9176696.html