Python 反射(reflection)
反射是指通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法
1.getattr(object, name, default = None)
根据字符串获取 obj 对象里对应 str 方法的内存地址
示例:
class Dog(object):
def __init__(self, name):
self.name = name def eat(self, food):
print('%s is eating %s' % (self.name, food)) dog1 = Dog('Dog1')
choice = input('>>').strip()
if hasattr(dog1, choice):
getattr(dog1, choice)('bone') # 根据字符串获取对象里对应方法的内存地址,传入‘bone’执行
print(getattr(dog1, choice)) # 打印属性
输出结果:
>>eat
Dog1 is eating bone
<bound method Dog.eat of <__main__.Dog object at 0x00000249C4C3B780>>
2.hasattr(object, name)
判断一个 obj 对象里是否有对应 str 字符串
示例:
class Dog(object):
def __init__(self, name):
self.name = name def eat(self, food):
print('%s is eating %s' % (self.name, food)) dog1 = Dog('Dog1')
choice = input('>>').strip()
print(hasattr(dog1, choice)) # 判断一个 obj 对象里是否有对应 str 字符串
输出结果:
>>eat
True
3.setattr(object, y, v)
给类新加了一个属性等于: obj.y = v
传入属性示例:
class Dog(object):
def __init__(self, name):
self.name = name def eat(self, food):
print('%s is eating %s' % (self.name, food)) dog1 = Dog('Dog1')
choice = input('>>').strip()
print(hasattr(dog1, choice)) # 判断一个 obj 对象里是否有对应 str 字符串
if hasattr(dog1, choice):
print(getattr(dog1, choice)) # 打印修改前的属性
setattr(dog1, choice, 'Dog2') # 如果属性存在,可以通过 setattr 进行修改
print(getattr(dog1, choice)) # 打印修改后的属性
else:
setattr(dog1, choice, None) # 给类新加了一个属性 == dog1.choice = None
print(getattr(dog1, choice)) # 打印新加入的属性
修改已有属性输出结果:
>>name
True
Dog1
Dog2
增加新的属性输出结果:
>>age
False
None
传入方法示例:
class Dog(object):
def __init__(self, name):
self.name = name def eat(self, food):
print('%s is eating %s' % (self.name, food)) def bulk(self): # 传入方法需要提前写好
print('%s: woof,woof!' % self.name) dog1 = Dog('Dog1')
choice = input('>>').strip()
print(hasattr(dog1, choice)) # 判断一个 obj 对象里是否有对应 str 字符串
if hasattr(dog1, choice):
pass
else:
setattr(dog1, choice, bulk) # 给类新加了一个方法
dog1.bulk(dog1) # 调用新加入的方法 bulk
输出结果:
>>bulk
False
Dog1: woof,woof!
4.delattr(object, name)
删除 obj 对象中对应属性
示例:
class Dog(object):
def __init__(self, name):
self.name = name def eat(self, food):
print('%s is eating %s' % (self.name, food)) dog1 = Dog('Dog1')
choice = input('>>').strip()
print(hasattr(dog1, choice)) # 输出 True
if hasattr(dog1, choice):
delattr(dog1, choice)
print(getattr(dog1, choice)) # 打印会报错
Python 反射(reflection)的更多相关文章
- python 反射
python 反射的核心本质其实就是利用字符串的形式去对象(模块)中操作(查找/获取/删除/添加)成员,一种基于字符串的事件驱动! 反射的四个基本函数使用 hasattr,getattr,setatt ...
- [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦
[.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...
- [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程
[.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程 本节导读:本节主要介绍什么是.NET反射特性,.NET反射能为我们做些什么,最后介绍几种常用的 ...
- python反射
python反射 python的反射是基于字符串的形式去对象(模块)中操作其成员.此操作是动态的,常用于web开发中url参数中对应模块或者函数的反射. 下面开始具体说明: 场景需求: 我的pytho ...
- [整理]C#反射(Reflection)详解
本人理解: 装配件:Assembly(程序集) 晚绑定:后期绑定 MSDN:反射(C# 编程指南) -----------------原文如下-------- 1. 什么是反射2. 命名空间与装配件的 ...
- CSharpGL(43)环境映射(Environment Mapping)-天空盒(Skybox)反射(Reflection)和折射(Refraction)
CSharpGL(43)环境映射(Environment Mapping)-天空盒(Skybox)反射(Reflection)和折射(Refraction) 开始 如图所示,本文围绕GLSL里的sam ...
- 代理(Proxy)和反射(Reflection)
前面的话 ES5和ES6致力于为开发者提供JS已有却不可调用的功能.例如在ES5出现以前,JS环境中的对象包含许多不可枚举和不可写的属性,但开发者不能定义自己的不可枚举或不可写属性,于是ES5引入了O ...
- Python反射机制理解
Python反射机制用沛齐老师总结的话说就是:利用字符串的形式去对象(模块)中操作(寻找)成员. getattr(object, name) object代表模块,name代表模块中的属性或成员,该函 ...
- Golang 反射reflection
反射reflection 反射可大大提高程序的灵活性,使得interface{}有更大的发挥余地 反射使用TypeOf和ValueOf函数从接口中获取目标对象信息 反射会将匿名字段作为独立字段(匿名字 ...
随机推荐
- ALV屏幕捕捉回车及下拉框事件&ALV弹出框回车及下拉框事件
示例展示: 屏幕依据输入的物料编码或下拉框物料编码拍回车自动带出物料描述: 点击弹出框,输入物料编码拍回车带出物料描述,点击确认,更新ALV: 1.创建屏幕9000,用于处理ALV弹出框: 2.针对屏 ...
- eclipse中访问不了tomcat首页server Locations变灰无法编辑
eclipse中访问不了tomcat首页server Locations变灰无法编辑 2014年07月25日 14:37:21 wuha0 阅读数:19139更多 个人分类: servlet 解决 ...
- Linux系统中安装Oracle数据库
安装前的准备 三个包:winx64_12201_database.zip(oracle数据库) window_7(安装在虚拟机中的window7纯净版系统) client.zip(oracle的监听器 ...
- Ubuntu 修改sudoers之后无法用sudo怎么恢复
进入终端 键入 pkexec visudo 修改sudoer.temp 实例如下 ## This file MUST be edited with the 'visudo' command as ro ...
- Labview-vi的可重入性
VI可重入性: labview多线程中 同时对一个子vi访问时,可能会造成同时对同一块内存地址读写所造成的数据混乱,当选择 vi属性(Ctrl+i)中执行选项卡允许可重入时,labview会分配不同的 ...
- wpf binging(五) 数据的转换与验证
1.数据的验证,有时候需要验证同步的数据是否正常 需要派生一个类 ValidationRule 再把这个类指定给binging 进行验证 在这里如果验证不通过 textbox就会变成红色并且发出警告数 ...
- 1--Test NG--常见测试和注解
第一:注解 (1)@test (2)@BeforeMethod,@AfterMethod (3)@BeforeClass,@AfterClass (4)@BeforeSuite,@AfterSuite ...
- SQL设置时间格式
SELECT STR_TO_DATE('Jul 20 2013 7:49:14:610AM','%b %d %Y %h:%i:%s:%f%p') from DUAL; -- 执行后得到结果:'2013 ...
- javascript中的Date对象
Date是什么? Date是日期类的构造函数 也是个对象,用于构造日期对象的实例. 有一个 now()方法,返回截止目前的时间戳(1970.1.1日始). Date.parse()接受 一定格式的日期 ...
- Mac 安装Python3 facewap环境
参考网上大神的方法 1 官网下载安装 2 下载指定版本的源码cmake安装 3 Mac上使用homebrew进行安装(强烈推荐,主要是前两种的openssl模块我没有搞定链接什么的一直报错,一个个下载 ...