多平台统一管理软件接口,如何实现多平台统一管理软件接口
228
2022-09-02
Python面试题之Python对象反射、类反射、模块反射(python 反射)
python面向对象中的反射:通过字符串的形式操作对象相关的属性。python中的一切事物都是对象(都可以使用反射)
一、getattr
对象获取
class Manager: role = "管理员" def __init__(self,name,sex,phone,mail): self.name = name self.sex = sex self.phone = phone self.mail = mail def createClass(self): print("create class") def createTeacher(self): print("createTeacher") def createStu(): print("createStu")manager = Manager("safly","男",123456,123456)print("---对象获取对象方法-----")f = getattr(manager,"createClass")f()print("---对象获取对象属性-----")name = getattr(manager,"name")print(name)print("---对象获取类属性-----")role = getattr(manager,"role")print(role)print("---对象获取类属性(可以设置默认值)-----")import loggingif hasattr(manager,"role1"): role = getattr(manager,"role1","roleDefault") print(role)else: logging.warn("没有role属性") role = getattr(manager, "role1", "roleDefault") print(role)
类获取
class Manager: role = "管理员" def createClass(self): print("create class") def createStu(): print("createStu")m = Manager()f = getattr(Manager,"createClass")f(Manager)f = getattr(Manager,"createClass")f(m)role = getattr(Manager,"createStu")role()#对象获取类属性role = getattr(Manager,"role")print(role)
输出如下:
create classcreate classcreateStu管理员
二、setattr
设置类属性、方法
class Manager: role = "管理员" def __init__(self,name,sex,phone,mail): self.name = name self.sex = sex self.phone = phone self.mail = mail def createClass(self): print("create class") def createTeacher(self): print("createTeacher") def createStu(): print("createStu")manager = Manager("safly","男",123456,123456)print("----设置类属性------")setattr(Manager,"country","china")print(Manager.country)print("----删除类属性------")delattr(Manager,"country")# #删除报错# print(Manager.country)print("----设置类方法------")def Method(parm): print("我是被绑定的class之外的方法parm--",parm)setattr(Manager,"Method",Method)Manager.Method("saf")Manager.Method(Manager)Manager.Method(manager)def Method(): print("我是被绑定的class之外的方法parm--")setattr(Manager,"Method",Method)Manager.Method()
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/demo.py----设置类属性------china----删除类属性----------设置类方法------我是被绑定的class之外的方法parm-- saf我是被绑定的class之外的方法parm--
设置对象属性、方法
class Manager: role = "管理员" def __init__(self,name,sex,phone,mail): self.name = name self.sex = sex self.phone = phone self.mail = mail def createClass(self): print("create class") def createTeacher(self): print("createTeacher") def createStu(): print("createStu")manager = Manager("safly","男",123456,123456)print("----设置对象属性------")setattr(manager,"age",20)print(manager.age)print("----删除对象属性------")delattr(manager,"age")# 'Manager' object has no attribute 'age'# print(manager.age)print("---对象不能删除类属性---")setattr(Manager,"country","china")print(Manager.country)# delattr(manager,"country")# print(Manager.country)print("----设置对象方法------")def create_course(self): print('创建了一个课程')setattr(manager,'create_course',create_course)manager.create_course(manager)def create_grade(): print('创建了一个班级')setattr(manager,'create_grade',create_grade)manager.create_grade()
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/demo.py----设置对象属性------20----删除对象属性---------对象不能删除类属性---china----设置对象方法------创建了一个课程创建了一个班级Process finished with exit code 0
三、模块反射
创建一个模块mokuai.py
a = 1def method(rag): print(rag) return "666"
然后在python.py中导入以上模块
import mokuaiprint(getattr(mokuai,"a"))method = getattr(mokuai,"method")ret = method(8888)print(ret)
输出如下:
18888666
四、反射本模块函数、变量
aa = 11def method(): print("---method---")import sysprint(sys.modules[__name__])print(getattr(sys.modules[__name__],"aa"))f = getattr(sys.modules[__name__],"method")f()
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/demo.py
参考
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~