Lesson0402_GetatrrWebsite.py
#!/usr/bin/env/python #-*-coding:utf-8-*- #Author:LingChongShi #查看源码Ctrl+左键 def index(): print('欢迎访问XX网站') def login(): print('登录成功') def logout(): print('退出登录') class People(object): country='China' def __init__(self): pass def people_info(self): print('People类中people_info函数')
Lesson0403_Getattr.py
#!/usr/bin/env/python #-*-coding:utf-8-*- #Author:LingChongShi #查看源码Ctrl+左键 ''' getattr():根据字符串的形式去某个模块中查找X函数 hasattr():根据字符串的形式去某个模块判断X函数是否存在 setattr():根据字符串的形式去某个模块设置X函数 delattr():根据字符串的形式去某个模块删除X函数 ''' import Lesson04_Package.Lesson0402_GetatrrWebsite '''getattr(object,name,default): 1、object:对象(模块) 2、name:属性(函数/方法) 3、default:无对应属性,返回的值, 4、有对应属性,返回对象属性值 ''' getder=getattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'index','-1') print(getder) getder() obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People() getclass=getattr(obj,'people_info','-1') getclass() '''hasattr(object,name): 1、object:对象(模块) 2、name:属性(函数/方法) 3、如果对象有该属性返回True,否则返回False ''' has=hasattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'login') print(has) obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People() hasclass=hasattr(obj,'people_info') print(hasclass) '''setattr(object,name,value): 1、object:对象(模块) 2、name:属性(函数/方法) 3、value:属性值 4、无返回值 ''' set=setattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'str','添加的字符串') has1=hasattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'str') print(has1) get1=getattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'str') print(get1) obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People() setclass=setattr(obj,'exit','退出') hascalss=hasattr(obj,'exit') print(hasclass) '''delattr(object,name): 1、object:对象(模块) 2、name:属性(函数/方法) 3、无返回值 ''' del1=delattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'logout') has2=hasattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'logout') print(has2) # get2=getattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'logout') # print(get2) obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People hasclass=hasattr(obj,'people_info') print(hasclass) delclass=delattr(obj,'people_info') hasclass=hasattr(obj,'people_info') print(hasclass)