当前位置 : 主页 > 编程语言 > python >

Python面试题之Python对象反射、类反射、模块反射

来源:互联网 收集:自由互联 发布时间:2022-06-15
python面向对象中的反射:通过字符串的形式操作对象相关的属性。python中的一切事物都是对象(都可以使用反射) 一、getattr 对象获取 class Manager: role = "管理员" def __init__(self,name,sex,p


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 logging
if 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 class
create class
createStu
管理员


二、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 '__main__.Manager'>
我是被绑定的class之外的方法parm-- <__main__.Manager object at 0x0343B4B0>
我是被绑定的class之外的方法parm--

Process finished with exit code 0


设置对象属性、方法

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 = 1
def method(rag):
print(rag)
return "666"

然后在python.py中导入以上模块

import mokuai
print(getattr(mokuai,"a"))
method = getattr(mokuai,"method")
ret = method(8888)
print(ret)

输出如下:

1
8888
666


四、反射本模块函数、变量

aa = 11
def method():
print("---method---")
import sys

print(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
<module '__main__' from 'E:/python/py_pro/demo.py'>
11
---method---

Process finished with exit code 0


​​参考​​







上一篇:Python面试题之容器(Collections)
下一篇:没有了
网友评论