Python中如何实现面向对象的反射机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计932个文字,预计阅读时间需要4分钟。
pythondef reflect_getattr(target, attribute_name): try: # 使用getattr获取目标对象的指定属性 attribute_value=getattr(target, attribute_name) return fFound attribute '{attribute_name}' with value: {attribute_value} except AttributeError: return fAttribute '{attribute_name}' not found in the target.
反射
根据字符串为参数(第二个参数),去对象或模块(第一个参数)中寻找与之同名的成员。
v = getattr(obj,"func")
hasattr # 根据字符串的形式,去判断对象中是否有成员。
本文共计932个文字,预计阅读时间需要4分钟。
pythondef reflect_getattr(target, attribute_name): try: # 使用getattr获取目标对象的指定属性 attribute_value=getattr(target, attribute_name) return fFound attribute '{attribute_name}' with value: {attribute_value} except AttributeError: return fAttribute '{attribute_name}' not found in the target.
反射
根据字符串为参数(第二个参数),去对象或模块(第一个参数)中寻找与之同名的成员。
v = getattr(obj,"func")
hasattr # 根据字符串的形式,去判断对象中是否有成员。

