反射一共有4个方法
hasattr、getattr、setattr、delattr
还是看例子
#反射
#判断object中有没有一个name字符串对应的方法或者属相
class BlackMedium:
feture = 'Ugly'
def __init__(self,name,addr):
self.name = name
self.addr = addr
def sell_hourse(self):
print('【%s】正在卖房子'%self.name)
def rent_hourse(self):
print('【%s】正在出租房子'%self.name)
f1 = BlackMedium('Alex','天露园')
#print(f1.__dict__)#{'name': 'Alex', 'addr': '天露园'}
print(f1.__dir__())
#['name', 'addr', '__module__', 'feture', '__init__', 'sell_hourse', 'rent_hourse',
# '__dict__', '__weakref__', '__doc__', '__repr__', '__hash__', '__str__', '__getattribute__',
# '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__',
# '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__',
# '__sizeof__', '__dir__', '__class__']#这个是f1里面包含的所有的内置方法
#这时候先看看hasattr怎么用的
'''
def hasattr(*args, **kwargs): # real signature unknown
"""
Return whether the object has an attribute with the given name.
This is done by calling getattr(obj, name) and catching AttributeError.
"""
pass
'''#这个是python中对这个函数的解释
print(hasattr(f1,'name'))# 这个打印结果是True
print(hasattr(f1,'__reduce__'))# True 这个也是True
print(hasattr(f1,'__init__'))# True 这个也是True
print(hasattr(f1,'1231'))# False 这个也是False
# 通过这几个结果,大致我们也看出来了,hasattr()这个函数是查看f1这个实例化的对象里面有没有这个方法或者属性,
# 如果有则返回一个True,没有返回False
'''
#我们下面接着说getattr这个函数,还是先看Python中对这个函数的解释
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
'''
print(getattr(f1,'name'))#Alex
#print(getattr(f1,'name',__default='Hello'))#Alex
#print(getattr(f1,'1'))#AttributeError: 'BlackMedium' object has no attribute '1'
print(getattr(f1,'1','hello'))#打印结果是hello,看这个函数的解释我们能知道当f1对象没有这个属性时,会返回一个hello
print(getattr(f1,'name','hello'))#Alex,如果当f1对象存在这个属性时,会返回这个属性的值
print(getattr(f1,'sell_hourse'))#<bound method BlackMedium.sell_hourse of <__main__.BlackMedium object at 0x02FA9290>>,
# 返回这个函数的内存地址
print(getattr(f1,'name'))#其实相当于f1.name
print(f1.name),print(f1.sell_hourse)
'''
#接着就是setattr
def setattr(x, y, v): # real signature unknown; restored from __doc__
"""
Sets the named attribute on the given object to the specified value.
setattr(x, 'y', v) is equivalent to ``x.y = v''
"""
pass
'''
setattr(f1,'age',11)#f1对象增加一个属性f1,f1属性的值为11和f1.age = 11一样
f1.min = 21
print(hasattr(f1,'age'))#True
print(hasattr(f1,'min'))#True
#同样我们可以用来添加函数
setattr(f1,'func',lambda x:x+'sb')
print(f1.name)
print(f1.func('min'))#minsb
print(getattr(f1,'func'))#<function <lambda> at 0x03300ED0>
print(getattr(f1,'func')('min'))#minsb
setattr(f1,'func1',lambda self:self.name+'sb')
print(f1.func1)#<function <lambda> at 0x029CB3D8>
print(f1.func1(f1))#Alexsb
#最后一个是delattr
'''
def delattr(x, y): # real signature unknown; restored from __doc__
"""
Deletes the named attribute from the given object.
delattr(x, 'y') is equivalent to ``del x.y''
"""
pass
'''
print(hasattr(f1,'func1'))#True
delattr(f1,'func1')#删除f1对象里面的func这个属性,和del f1.func1一样
print(hasattr(f1,'func1'))#False
print(hasattr(f1,'func'))#True
del f1.func
print(hasattr(f1,'func'))#False
#delattr(f1,'func2')#如果本来不存在这个func2这个属性,则提示AttributeError: func2
del f1.func3#这个和上面原因一样AttributeError: func3
#好了,说到这里反射的一些基本的用法就说完了,暂时到这里,后续再见