判断类型
函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str、list、dict,也可以用在我们自定义的类,它们本质上都是数据类型。 假设有如下的 Person、Student 和 Teacher 的定义及继承关系如下: class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher, self).__init__(name, gender)
self.course = course p = Person('Tim', 'Male')
s = Student('Bob', 'Male', )
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()判断来验证您的答案。
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher, self).__init__(name, gender)
self.course = course t = Teacher('Alice', 'Female', 'English') print isinstance(t, Person)
print isinstance(t, Student)
print isinstance(t, Teacher)
print isinstance(t, object)

python 类型判断-- isinstance函数的更多相关文章

  1. Python 中的isinstance函数

    解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...

  2. python issubclass 和 isinstance函数

    Python issubclass() 函数 issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类. 语法: issubclass(class, clas ...

  3. Python 如何判断一个函数是generator函数?

    如何判断一个函数是否是一个特殊的 generator 函数?可以利用 isgeneratorfunction 判断: >>>from inspect import isgenerat ...

  4. Python中的isinstance函数

    isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo)   如果参数object是classinfo的实例,或者object是class ...

  5. YUI的类型判断函数

    1.首先定义一个关于类型的对象,及相关变量 类型判断对象 ar L = Y.Lang || (Y.Lang = {}), STRING_PROTO = String.prototype, TOSTRI ...

  6. 【跟着子迟品 underscore】常用类型判断以及一些有用的工具方法

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  7. Python 字符串内置函数(四)

    # 4.类型判断# isalnum()函数检测字符串是否只由字母和数字组成.s = "this2009"; # 字符中没有空格print(s.isalnum()) # 结果:Tru ...

  8. python中判断对象类型的函数——isinstance

    isinstance是Python中的一个内建函数.是用来判断一个对象的变量类型. isinstance(object, class-or-type-or-tuple) 如果参数object是clas ...

  9. python的数据结构分类,以及数字的处理函数,类型判断

    python的数据结构分类: 数值型 int:python3中都是长整形,没有大小限制,受限内存区域的大小 float:只有双精度型 complex:实数和虚数部分都是浮点型,1+1.2J bool: ...

随机推荐

  1. mybatis,批量新增、修改,删除

    转载自:http://blog.csdn.net/sanyuesan0000/article/details/19998727 最近需要用到Mybatis批量新增oracle数据库,刚开始在网上找到的 ...

  2. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【python】判断字符串日期是否有效

    来源: http://www.jb51.net/article/66014.htm http://www.runoob.com/python/att-time-strptime.html 用time模 ...

  4. rsync实现同步

    一.备份客户端: 1.创建/etc/rsyncd.secrets 权限配置600 (写服务器端的账户密码) 2.客户端配置文件: port=873log file=/var/log/rsync.log ...

  5. Android Studio新建了一个项目看不到手机界面的效果

    我今天新建了一个项目,但是在这里却看不到手机的界面效果,如下图:

  6. September 29th 2016 Week 40th Thursday

    Prosperity discovers vice, adversity virtue. 得意时露瑕疵,逆境中见品质. I wish I would have someone like you, fr ...

  7. jvm学习

    一.jps主要用来输出JVM中运行的进程状态信息 jps [options] [hostid] 如果不指定hostid就默认为当前主机或服务器. Jps -ml 二.jstack主要用来查看某个Jav ...

  8. 矿场搭建(codevs 1996)

    题目描述 Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使 ...

  9. Mysql之mysqlbinlog使用

    mysqlbinlog用于BinLog的显示,备份和重做. 默认情况下,mysqlbinlog是以base-64编码的方式呈现的.如: mysqlbinlog  master_bin.000006   ...

  10. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...