#The diary of a Python learner
Date:08.03.2024
Focus:OOP中关于获取对象信息的方法
Key:
type(xxx)==type(yyy)该方法可以判断函数/串/数值
isinstance(h, Husky)#可以判断是否属于某些类型;
isinstance([1, 2, 3], (list, tuple))#可以判断多子类,优先使用
>>>hasattr(obj, 'x') # 有属性'x'吗?
True
>>> obj.x
9
>>> hasattr(obj, 'y') # 有属性'y'吗?
False
>>> setattr(obj, 'y', 19) # 设置一个属性'y'
>>> hasattr(obj, 'y') # 有属性'y'吗?
True
>>> getattr(obj, 'y') # 获取属性'y'
19
>>> obj.y # 获取属性'y'
19
Practice:
1.
# -*- coding: utf-8 -*-
import
2.
# -*- coding: utf-8 -*-
import
Difficulty?
Addition:通过has/set/getattr实现对内在属性的判断/设置/获取