Isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。

isinstance() 与 type() 区别:

    type() 不会认为子类是一种父类类型,不考虑继承关系。

    isinstance() 会认为子类是一种父类类型,考虑继承关系。

    如果要判断两个类型是否相同推荐使用 isinstance()。
class A:
pass class B(A):
pass isinstance(A(), A) # returns True
type(A()) == A # returns True
isinstance(B(), A) # returns True
type(B()) == A # returns False

实际使用

     if not isinstance(value, int):
raise ValueError('score must be an integer!')
class Student(object):

    def get_score(self):
return self._score def set_score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value

[py]py里的isinstance判断实例来源(含父类)的更多相关文章

  1. Jupyter notebook导入Pycharm项目的.py文件里的模块及方法

    Jupyter notebook导入Pycharm项目种的.py文件里的模块及方法 需要在Jupyter notebook里调用自己写的代码,过程如下. 首先在Pycharm里写好一个文件,例如DCC ...

  2. js如何判断访问来源是来自搜索引擎(蜘蛛人)还是直接访问

    以下javascript脚本代码可以实现判断访问是否来自搜索引擎.代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <scri ...

  3. python3 isinstance()判断元素是否是字符串、int型、float型

    python3 isinstance()判断元素是否是字符串.int型.float型 isinstance是Python中的一个内建函数 语法: isinstance(object, classinf ...

  4. type,isinstance判断一个变量的数据类型

    type,isinstance判断一个变量的数据类型 import types type(x) is types.IntType # 判断是否int 类型 type(x) is types.Strin ...

  5. ORACLE里怎么能判断一个日期类型的字段是否为空,解决方法:is null

    ORACLE里怎么能判断一个日期类型的字段是否为空,解决方法:is null,解决方法:判断什么null都可以用is null.

  6. DHCP配置实例(含DHCP中继代理)

    https://blog.51cto.com/yuanbin/109759. DHCP配置实例(含DHCP中继代理)   某公司局域网有192.168.1.0/24和192.168.2.0/24这两个 ...

  7. Python3基础 isinstance 判断是否为指定类的实例对象

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. py定义变量-循环-条件判断

    定义变量 # print('hahaha')name = " let'go "title = '刘伟长得 "很帅"!'conent = '''     let' ...

  9. isinstance判断某个对象是否是某个类创建的

    #!/usr/bin/env python li = [11,22] #判断某个对象是否是某个类创建的. r = isinstance(li, list) print(r) 结果: C:\Python ...

随机推荐

  1. 【Scikit】实现Multi-label text classification代码模板

    Refer to: https://stackoverflow.com/a/10527953 code: # -*- coding: utf-8 -*- import numpy as np from ...

  2. http statusCode(状态码)含义

    201-206都表示服务器成功处理了请求的状态代码,说明网页可以正常访问. 200(成功) 服务器已成功处理了请求.通常,这表示服务器提供了请求的网页. 201(已创建) 请求成功且服务器已创建了新的 ...

  3. Thrift的一些概念

    Thrift最初是由Facebook开发的,因为随着流量和网络结构的扩展,一些操作如搜索.分发.事件日志记录等已经超出系统的处理范围,所以Facebook的工程师开发服务时选择了多种不同的编程语言来达 ...

  4. maven安装与创建多模块项目

    最新版已同步至 http://yywang.info/2014/05/31/maven-install-and-create-project/ maven是一个比较流行的项目管理工具,在最近参与的项目 ...

  5. easyui---tree异步加载

    1.ul li的多级列表的html代码tree 2.利用jquery <ul id="tt"></ul> $('#tt').tree({ url:'tree ...

  6. linux下安装svn出现configure: error: We require OpenSSL; try --with-openssl

    linux下安装svn出现configure: error: We require OpenSSL; try --with-openssl http://blog.csdn.net/woshixion ...

  7. Warning: Failed to halt at after bootloader, forced stop at

    该错误证实是因为 cc2650 SW下载模式,芯片复位引脚未接出来导致,芯片复位必须和下载器保持良好连接

  8. [No0000E7]C# 封装 与访问修饰符

    C# 支持的访问修饰符: Public Private Protected Internal Protected internal Public 访问修饰符 Public 访问修饰符允许一个类将其成员 ...

  9. 一些shell默认的变量

    测试脚本如下,我这里主要想测试$0,$1,$2,$n,$@,$*默认都代表了什么? #!/bin/sh echo '$1='$1 echo '$2='$2 echo '$@='$@ echo '$*= ...

  10. English class 81:How Vulnerability can make our lives better?

    1,can we come off as weak if we show imperfections? 2,The first thing I look for in you and the last ...