面向对象反射

三. 今日主要内容
1. issubclass, type, isinstance
issubclass 判断xxx类是否是xxx类的子类
type 获取到xxx对象的类型
isinstance 判断xxx对象是否是xxx类型的(向上判断)

2. 如何判断一个方法或者一个函数(FunctionType, MethodType)
from types import FunctionType, MethodType
print(isinstance(xx, FunctionType)))
print(isinstance(xx, MethodType)))

结论 :
1. 实例方法:
用类名访问. 函数
用对象访问. 方法
2. 静态方法
都是函数
3. 类方法
都是方法
3. 反射(重点)
hasattr(对象, 属性(字符串))
getattr(对象, 属性(字符串)) 从对象中获取到xxx属性

setattr(对象, 属性, 值)
delattr(对象, 属性) 从对象中删除xxx属性

4. md5加密
import hashlib
obj = hashlib.md5(加盐)
obj.update(铭文的bytes)
obj.hexdigest() 获取密文

原文地址:https://www.cnblogs.com/wwjx/p/9937102.html