使用例子
class Person:
sex = 'male'
def __init__(self,name):
self.name = name
def sayHi(self):
print('hi, '+self.name+'!')
@classmethod
def show_Type(cls):
print(cls.sex)
hsr = Person('hsr')
#判断是否存在
has_arg = hasattr(hsr,'name')
print(has_arg)
has_method = hasattr(hsr,'sayHi')
print(has_method)
#获取
name = getattr(hsr,'name')
print(name)
func = getattr(hsr,'sayHi')
func()
#设置
setattr(hsr,'name','cool')
setattr(hsr,'sayHi',lambda self:self.name)
#删除
delattr(hsr,'name')
delattr(hsr,'sayHi')
#反射类、模块的属性和方法相同,将对象换成对应的名字就行
#获取自己模块的名字,使用sys.modules[__name__]