Python的反射

通过字符串的形式操作对象的属性

python 复制代码
class Person(object):
    def __init__(self,name,age) -> None:
        self.name=name
        self.age=age
    def walking(self):
        print("walking......")

获取对象的属性

python 复制代码
p=Person("Recardo",12)
# 获取队形的name属性
a=getattr(p,"name")
# Recardo
print(a)

为对象添加static的属性

python 复制代码
p=Person("Recardo",12)
# 添加static 属性的变量
setattr(p,"yanling","show me the flowers")
# show me the flowers
print(p.yanling)

为对象添加方法

python 复制代码
p=Person("Recardo",12)
#对实例添加方法
def talking(self):
    print(self.name,"is talking")
# 为p对象添加名为speak的方法
setattr(p,"speak",talking)
# Recardo is talking
p.speak(p)

对Student类添加方法

python 复制代码
p=Person("Recardo",12)
#对实例添加方法
def talking(self):
    print(self.name,"is talking")
#对Person类添加方法
setattr(Person,"speak2",talking)
# p对象调用Person类的方法
# Recardo is talking
p.speak2()

删除对象的变量

python 复制代码
p=Person("Recardo",12)
# 相当于del p.age
delattr(p,"age") 
# AttributeError: 'Person' object has no attribute 'age'
print(p.age)

输入对象的方法名,对象执行方法

python 复制代码
class Person(object):
    def __init__(self,name,age) -> None:
        self.name=name
        self.age=age
    def walking(self):
        print(self.name," is walking......")

p=Person("Recardo",12)
user_command=input(">>:").strip()
if hasattr(p,user_command):
    func=getattr(p,user_command)
    func()
"""
>>:walking
Recardo  is walking......
"""

__name__属性

reflect2.py

python 复制代码
"""
reflect2.py
magical reflect2
__name__
反射一个.py文件下的字符串的属性
"""

class Person(object):
    def __init__(self,name,age) -> None:
        self.name=name
        self.age=age
    def walking(self):
        print("walking......")


print("outside the __main__")
print(__name__)
#在被别的模块导入的时候,发挥作用,被导入时,__name__=="xxx.py"中的xxx
#不被导入时,__name__=="__main__"
if __name__=="__main__":
    print("inside the __main__")
    print(__name__)
"""
outside the __main__
__main__
inside the __main__
__main__
"""

此时,无论是main方法里面还是main方法外面,name__都等于__main

reflect4.py

python 复制代码
import reflect2 as r2

运行reflect4.py

控制台打印:

python 复制代码
outside the __main__
reflect2

此时,reflect2中的name是reflect2

夸文件反射

python 复制代码
# reflect4.py
# 导入reflect2.py
import reflect2 as r2

if hasattr(r2,"Person"):
    PersonClass=getattr(r2,"Person")
    p=PersonClass("ChenLuo",3)
    print(p.name)
    print(p.age)
    p.walking()

"""
运行结果:
outside the __main__
reflect2
ChenLuo
3
walking......
"""
相关推荐
艾莉丝努力练剑30 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
橡晟4 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子4 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
企鹅与蟒蛇5 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba5 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上6 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab