python中判断类型
来源:互联网 发布:ovs linux网桥 编辑:程序博客网 时间:2024/05/22 02:24
函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str、list、dict,也可以用在我们自定义的类,它们本质上都是数据类型。
假设有如下的 Person、Student 和 Teacher 的定义及继承关系如下:
class Person(object): def __init__(self, name, gender): self.name = name self.gender = genderclass Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = scoreclass Teacher(Person): def __init__(self, name, gender, course): super(Teacher, self).__init__(name, gender) self.course = coursep = Person('Tim', 'Male')s = Student('Bob', 'Male', 88)t = Teacher('Alice', 'Female', 'English')
当我们拿到变量 p、s、t 时,可以使用 isinstance 判断类型:
>>> isinstance(p, Person)True # p是Person类型>>> isinstance(p, Student)False # p不是Student类型>>> isinstance(p, Teacher)False # p不是Teacher类型
这说明在继承链上,一个父类的实例不能是子类类型,因为子类比父类多了一些属性和方法。
我们再考察 s :
>>> isinstance(s, Person)True # s是Person类型>>> isinstance(s, Student)True # s是Student类型>>> isinstance(s, Teacher)False # s不是Teacher类型
s 是Student类型,不是Teacher类型,这很容易理解。但是,s 也是Person类型,因为Student继承自Person,虽然它比Person多了一些属性和方法,但是,把 s 看成Person的实例也是可以的。
这说明在一条继承链上,一个实例可以看成它本身的类型,也可以看成它父类的类型。
任务
请根据继承链的类型转换,依次思考 t 是否是 Person,Student,Teacher,object 类型,并使用isinstance()判断来验证您的答案。
- ?不会了怎么办
注意t 是Teacher的实例,继承链是:
object <- Person <- Teacher
参考代码:
class Person(object): def __init__(self, name, gender): self.name = name self.gender = genderclass Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = scoreclass Teacher(Person): def __init__(self, name, gender, course): super(Teacher, self).__init__(name, gender) self.course = courset = Teacher('Alice', 'Female', 'English')print isinstance(t, Person)print isinstance(t, Student)print isinstance(t, Teacher)print isinstance(t, object)
0 0
- python中判断类型
- python中如何判断字符类型
- Python中判断输入数据的类型
- Python中使用isinstance()判断变量类型
- python 数值类型,判断类型
- Python类型判断
- Python判断对象类型
- python判断操作系统类型
- 布尔类型判断-python
- python判断操作系统类型
- python 判断类型
- python 类型判断
- python判断操作系统类型
- python 判断变量类型
- 【python】判断字符串类型
- python判断操作系统类型
- Python 判断变量类型
- Python判断变量类型
- Ajax中同步与异步区别
- Jiecaovideo开源项目使用
- 深拷贝
- ElasticSearch 数组搜索的资料
- 生产者与消费者
- python中判断类型
- Swift3-UISearchController的基本使用
- Android-TextView
- 搭建keepalived遇到的问题
- Python 强大的模式匹配
- Jquery中find与each方法用法实例
- Android一些工具方法
- Oracle 调整SGA、PGA大小
- Hack Team