反射
【一】概要
- 在Python中,反射指的是通过字符串来操作对象的属性,涉及四个内置函数的使用(Python中一切皆对象,类和对象都可以用)
【二】常用方法
- getattr(object, name[, default])
- hasattr(object, name)
- setattr(object, name, value)
- delattr(object, name)
【三】详解
【1】getattr(object, name[, default])
:获取对象的属性值
| """ |
| getattr(object, name[, default]) -> value |
| |
| Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. |
| When a default argument is given, it is returned when the attribute doesn't |
| exist; without it, an exception is raised in that case. |
| """ |
| |
| class Person: |
| eyes = 2 |
| |
| def __init__(self, name, age): |
| self.name = name |
| self.age = age |
| |
| |
| '''实例化对象''' |
| p = Person('user', 20) |
| |
| res = getattr(p, 'eyes') |
| res1 = getattr(p, 'name') |
| print(res) |
| print(res1) |
| |
| '''如果是不存在的属性,且不指定默认值,将会报错''' |
| |
| |
| |
| '''设置默认值''' |
| res3 = getattr(p, 'gender', 'male') |
| print(res3) |
| print(p.__dict__) |
| |
| res4 = getattr(p, 'name', '001') |
| print(res4) |
| '''getattr的函数用法''' |
| class Person: |
| eyes = 2 |
| |
| def __init__(self, name, age): |
| self.name = name |
| self.age = age |
| |
| def func(self): |
| print('from func') |
| |
| def func_exists(self): |
| print('from func_exists') |
| |
| |
| p = Person('user', 20) |
| res = getattr(p, 'func') |
| print(res) |
| res() |
| |
| '''函数用法2:可以将不存在的函数名,通过指定内存地址,打印出''' |
| res1 = getattr(p, 'func_not', p.func_exists) |
| print(res1) |
| res1() |
| |
| class School(object): |
| def fault(self): |
| print("from fault") |
| |
| def not_fault(self): |
| print("cant fund func") |
| |
| |
| s = School() |
| s.fault() |
| '''当调用一个不存在的属性时,正常调用将会报错''' |
| |
| '''如果不存在,将会输出我们指定的内容''' |
| res = getattr(s, 'true', s.not_fault) |
| res() |
| '''如果存在,将正常的输出函数''' |
| res1 = getattr(s, 'fault', s.not_fault) |
| res1() |
【2】hasattr(object, name)
:判断对象是否具有指定属性
| """ |
| Return whether the object has an attribute with the given name. |
| |
| This is done by calling getattr(obj, name) and catching AttributeError. |
| """ |
| '''返回布尔值''' |
| class School(object): |
| def func(self): |
| print("from func") |
| |
| |
| s = School() |
| res = hasattr(s, 'func') |
| print(res) |
| res1 = hasattr(s, 'index') |
| print(res1) |
【3】setattr(object, name, value)
:设置对象的属性值
| """ |
| Sets the named attribute on the given object to the specified value. |
| |
| setattr(x, 'y', v) is equivalent to ``x.y = v'' |
| """ |
| class School(object): |
| def func(self): |
| print("from func") |
| |
| |
| def index(): |
| print("from outer func.index") |
| |
| |
| s = School() |
| '''如果已经存在,将会替换''' |
| res = setattr(s, 'func', 'func.index') |
| print(res) |
| '''如果不存在,将会新增''' |
| setattr(s, 'index', index) |
| print(s.__dict__) |
| |
| s.index() |
【4】delattr(object, name)
:删除对象的属性
| """ |
| Deletes the named attribute from the given object. |
| |
| delattr(x, 'y') is equivalent to ``del x.y'' |
| """ |
| class School(object): |
| name = 'school' |
| |
| def func(self): |
| print("from func") |
| |
| |
| print(School.name) |
| School.func(self=School()) |
| print(School.__dict__) |
| |
| |
| '''删除属性值''' |
| delattr(School, 'name') |
| delattr(School, 'func') |
| print(School.__dict__) |
| |
| |
| '''调用删除后的值,将会报错''' |
| print(School.name) |
| School.func(School()) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了