Python isinstance()

2023-05-16

Python isinstance() function is used to check if an object is an instance of the specified class or not.

Python的isinstance()函数用于检查对象是否为指定类的实例。

Python isinstance() (Python isinstance())

Python isinstance() function syntax is:

Python的isinstance()函数语法为:

isinstance(object, classinfo)

This function returns True if the object is instance of classinfo argument or instance of classinfo subclass.

如果对象是classinfo参数的实例或classinfo子类的实例,则此函数返回True

If the object is not an instance of classinfo or its subclass, then the function returns False.

如果对象不是classinfo或其子类的实例,则该函数返回False

classinfo argument can be a tuple of types. In that case, isinstance() will return True if the object is an instance of any of the types.

classinfo参数可以是类型的元组 。 在这种情况下,如果对象是任何类型的实例,则isinstance()将返回True。

If classinfo is not a type or tuple of types, a TypeError exception is raised.

如果classinfo不是类型或类型的元组,则会引发TypeError异常。

Python isinstance()示例 (Python isinstance() example)

Let’s look at some simple examples of isinstance() function with built-in data types, such as string, tuple, list, dict, bytes etc.

让我们看一下具有内置数据类型的isinstance()函数的一些简单示例,例如字符串 ,元组, 列表 , 字典 , 字节等。

Python isinstance()编号 (Python isinstance() number)

i = 10
print('i is int:', isinstance(i, int))

f = 10.5
print('f is float:', isinstance(f, float))

Output:

输出:

i is int: True
f is float: True

Python isinstance()字符串 (Python isinstance() string)

s = 'a'
print('s is str:', isinstance(s, str))

Output:

输出:

s is str: True

Python isinstance()字节 (Python isinstance() bytes)

b = bytes('abc', 'utf-8')
print('b is bytes:', isinstance(b, bytes))

Output:

输出:

b is bytes: True

Python isinstance()元组 (Python isinstance() tuple)

t = (1, 2)
print('t is tuple:', isinstance(t, tuple))

Output:

输出:

t is tuple: True

Python isinstance()列表 (Python isinstance() list)

li = []
print('li is list:', isinstance(li, list))

Output:

输出:

li is list: True

Python isinstance()字典 (Python isinstance() dict)

d = {}
print('d is dict:', isinstance(d, dict))

Output:

输出:

d is dict: True

Python isinstance()类和继承 (Python isinstance() class and inheritance)

Let’s look at an example of isinstance() function with custom class and inheritance with multiple classes.

我们来看一个带有自定义类和多个类继承的isinstance()函数的示例。

class Person:
    name = ''


class Employee(Person):
    id = 0


p = Person()
e = Employee()

print('p isinstance of Person:', isinstance(p, Person))
print('p isinstance of Employee:', isinstance(p, Employee))

print('e isinstance of Person:', isinstance(e, Person))
print('e isinstance of Employee:', isinstance(e, Employee))

Output:

输出:

p isinstance of Person: True
p isinstance of Employee: False
e isinstance of Person: True
e isinstance of Employee: True

Python isinstance()类的元组 (Python isinstance() tuple of classes)

print('p is an instance of Person or Employee:', isinstance(p, (Person, Employee)))
print('e is an instance of Person or Employee:', isinstance(e, (Person, Employee)))

Output:

输出:

p is an instance of Person or Employee: True
e is an instance of Person or Employee: True

摘要 (Summary)

Python isinstance() is a utility function to check if an object is of a specific type or not. We can use it to perform a type check and perform operations based on the type of object.

Python isinstance()是一种实用程序函数,用于检查对象是否为特定类型。 我们可以使用它来执行类型检查并根据对象的类型执行操作。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22932/python-isinstance

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python isinstance() 的相关文章

  • 如何更改 FacetGrid 中的边距标题颜色

    使用 Seaborn Facet Grids 如何仅更改边距标题的颜色 注意g set titles color red 更改两个标题 p sns load dataset penguins sns displot data p x fli
  • 从 SHAP 值中获取特征重要性

    我想要获得重要功能的数据框 通过下面的代码 我得到了 shap values 但我不确定这些值的含义是什么 在我的 df 中有 142 个特征和 67 个实验 但得到了一个带有 ca 的数组 2500 个值 explainer shap T
  • 使用 Python 创建 MIDI

    本质上 我正在尝试从头开始创建 MIDI 并将它们放到网上 我对不同的语言持开放态度 但更喜欢使用Python 两种语言之一 如果这有什么区别的话 并且想知道我应该使用哪个库 提前致谢 看起来这就是您正在寻找的 适用于 Python 的简单
  • ctypes 错误:libdc1394 错误:无法初始化 libdc1394

    我正在尝试将程序编译为共享库 我可以使用 ctypes 在 Python 代码中使用该库 使用以下命令该库可以正常编译 g shared Wl soname mylib O3 o mylib so fPIC files pkg config
  • Pyqt-如何因另一个组合框数据而更改组合框数据?

    我有一个表 有 4 列 这 4 列中的两列是关于功能的 一个是特征 另一个是子特征 在每一列中 所有单元格都有组合框 我可以在这些单元格中打开txt 我想 当我选择电影院作为功能时 我只想看到子功能组合框中的电影名称 而不是我的 数据 中的
  • Python 是解释型的还是编译型的,或者两者兼而有之?

    据我了解 An 解释的语言是由解释器 将高级语言转换为机器代码然后执行的程序 实时运行和执行的高级语言 它一次处理一点程序 A compiled语言是一种高级语言 其代码首先由编译器 将高级语言转换为机器代码的程序 转换为机器代码 然后由执
  • 字符串中的注释和注释中的字符串

    我正在尝试使用 Python 和 Regex 计算 C 代码中包含的注释中的字符数 但没有成功 我可以先删除字符串以删除字符串中的注释 但这也会删除注释中的字符串 结果会很糟糕 是否有机会通过使用正则表达式来询问不匹配注释中的字符串 反之亦
  • ValueError:不支持连续[重复]

    这个问题在这里已经有答案了 我正在使用 GridSearchCV 进行线性回归的交叉验证 不是分类器也不是逻辑回归 我还使用 StandardScaler 对 X 进行标准化 我的数据框有 17 个特征 X 和 5 个目标 y 观察 约11
  • CNTK 抱怨 LSTM 中的动态轴

    我正在尝试在 CNTK 中实现 LSTM 使用 Python 来对序列进行分类 Input 特征是固定长度的数字序列 时间序列 标签是 one hot 值的向量 Network input input variable input dim
  • 在相同任务上,Keras 比 TensorFlow 慢

    我正在使用 Python 运行斩首 DCNN 本例中为 Inception V3 来获取图像特征 我使用的是 Anaconda Py3 6 和 Windows7 使用 TensorFlow 时 我将会话保存在变量中 感谢 jdehesa 并
  • Alembic:如何迁移模型中的自定义类型?

    My User模型是 class User UserMixin db Model tablename users noinspection PyShadowingBuiltins uuid Column uuid GUID default
  • 揭秘sharedctypes性能

    在 python 中 可以在多个进程之间共享 ctypes 对象 然而我注意到分配这些对象似乎非常昂贵 考虑以下代码 from multiprocessing import sharedctypes as sct import ctypes
  • 对使用 importlib.util 导入的对象进行酸洗

    我在使用Python的pickle时遇到了一个问题 我需要通过将文件路径提供给 importlib util 来加载一些 Python 模块 如下所示 import importlib util spec importlib util sp
  • 如何在Python中按AaB而不是ABa顺序对字符串进行排序

    我正在尝试对字符串进行排序 为 punnetsquare 制作基因型 我目前的实现是 unsorted genotype ABaB sorted genotype sorted list unsorted genotype sorted s
  • 使用 Conda 更新特定模块会删除大量软件包

    我最近开始使用 Anaconda Python 发行版 因为它提供了许多开箱即用的数据分析库 使用 conda 创建环境和安装软件包也轻而易举 但是当我想更新 Python 本身或任何其他模块时 我遇到了一些严重的问题 我事先被告知我的很多
  • 在 Spyder 的变量资源管理器中查看局部变量

    我是 python 新手 正在使用 Spyder 的 IDE 我欣赏它的一项功能是它的变量资源管理器 然而 根据一些研究 我发现它只显示全局变量 我找到的解决方法是使用检查模块 import inspect local vars def m
  • Django - 提交具有同一字段多个输入的表单

    预警 我对 Django 以及一般的 Web 开发 非常陌生 我使用 Django 托管一个基于 Web 的 UI 该 UI 将从简短的调查中获取用户输入 通过我用 Python 开发的一些分析来提供输入 然后在 UI 中呈现这些分析的可视
  • 使用 NLP 进行地址分割

    我目前正在开发一个项目 该项目应识别地址的每个部分 例如来自 str Jack London 121 Corvallis ARAD ap 1603 973130 输出应如下所示 street name Jack London no 121
  • 计算互相关函数?

    In R 我在用ccf or acf计算成对互相关函数 以便我可以找出哪个移位给我带来最大值 从它的外观来看 R给我一个标准化的值序列 Python 的 scipy 中是否有类似的东西 或者我应该使用fft模块 目前 我正在这样做 xcor
  • bs4 `next_sibling` VS `find_next_sibling`

    我在使用时遇到困难next sibling 并且类似地与next element 如果用作属性 我不会得到任何返回 但如果用作find next sibling or find next 然后就可以了 来自doc https www cru

随机推荐

  • 设置成功的开源计划办公室(OSPO)的指南

    公司创建了开放源代码计划办公室 xff08 OSPO xff09 xff0c 以管理其与所依赖的开放源代码生态系统的关系 通过了解公司的开源生态系统 xff0c OSPO可以最大化公司的投资回报率 xff0c 并降低使用 xff0c 贡献和
  • 开源搜索引擎 种子搜索_使用开源搜索引擎自定义您的互联网

    开源搜索引擎 种子搜索 很久以前 xff0c 互联网很小 xff0c 只有几个人可以将它们编入索引 xff0c 这些人收集了所有网站的名称和位置 xff0c 并按页面或印刷书籍中的主题列出了它们 随着万维网网络的发展 xff0c 网络响动
  • 开源 apm_使用开源APM软件:InspectIT

    开源 apm 在当今时代 xff0c 软件系统不断变得越来越复杂 同时 xff0c 客户对响应时间和可用性的期望比以往更高 如您所知 xff0c 性能不佳的服务可能会将客户吸引到竞争对手的产品中 因此 xff0c 系统故障和性能不佳通常会对
  • 啦啦啦啦啦_开放组织读书俱乐部:啦啦队长如何设定方向

    啦啦啦啦啦 我们终于进入了为期7周的开放式组织虚拟图书俱乐部的第7章 xff0c 催化方向 在前几周 xff0c 我们讨论了开放组织的原因和方式 与启动上周的包容性决策的讨论 xff0c 我们一头扎进了 自下而上 的组织模式是什么成分 现在
  • cisco路由器vty_如何使用VTY Shell配置路由器

    cisco路由器vty 最近 xff0c 我写了一篇文章 xff0c 解释了如何使用Quagga路由套件实现开放式最短路径优先 xff08 OSPF xff09 可以使用多个软件套件代替Quagga来实现不同的路由协议 一种这样的选择是自由
  • powerdns_使用PowerDNS为名称服务器轻松配置DNS

    powerdns 几个月前 xff0c 我们要求为新项目提供稳定可靠的域名系统 xff08 DNS xff09 服务器 该项目使用容器进行自动部署 xff0c 每个新环境将在其中生成唯一的随机URL 在对可能的解决方案进行了大量研究之后 x
  • 人工智能革命(下):永生还是毁灭

    导读 xff1a 本系列文章讲述了人工智能革命的爆发以及人类未来的出路 xff0c 由于篇幅较长分为上下两篇 xff0c 原英文载于神奇的网站 WaitButWhy com xff0c 作者Tim Urban还写过一篇有关脑机接口的文章 N
  • rust vs java_为什么我喜欢以Java程序员的身份学习Rust

    rust vs java 自从我正确地学习了计算机或人类这门新语言以来 xff0c 已经很长时间了 也许25年 那是Java语言 xff0c 尽管与此同时我不得不写一点点C xff08 很少 xff09 和JavaScript xff0c
  • git-cola使用教程_使用Git Cola轻松实现Git

    git cola使用教程 Git是一个Linux命令 xff0c 可帮助您管理工作的版本 它已被移植到BSD xff0c macOS xff0c Windows等 它是流行的代码托管服务的基础 xff0c 包括GitLab和NotABug等
  • .net 开发使用什么语言_如何开始使用.NET开发

    net 开发使用什么语言 NET框架由Microsoft在2000年发布 该平台的开源实现Mono在2000年代初一直是争议的中心 xff0c 因为Microsoft拥有 NET技术的多项专利 xff0c 并且可以使用这些专利终止Mono的
  • linux重启命令_3条命令重启Linux(另外4种安全方式)

    linux重启命令 Linux完全有能力运行 xff0c 而不需要数周 xff0c 而是数年 xff0c 而无需重新启动 在某些行业中 xff0c 这正是Linux的功能 xff0c 这要归功于kpatch和kgraph之类的进步 但是 x
  • crazy pony_Pony编程语言简介

    crazy pony 在Wallaroo Labs xff08 我是工程副总裁 xff09 xff0c 我们正在构建以Pony编程语言编写的高性能 xff0c 分布式流处理器 大多数人都没有听说过Pony xff0c 但是对于Wallaro
  • html标记语言图像标记_为什么我喜欢这些标记语言

    html标记语言图像标记 去年大约这个时候 xff0c 我为本专栏文章简要介绍了各种标记语言 语言选择的话题最近出现了好几次 xff0c 所以我认为现在该是时候以我的偏见来重新讨论这个话题了 我在这里解释为什么我更喜欢我的语言 xff0c
  • 无人机开源项目_8个开源无人机项目

    无人机开源项目 编者注 xff1a 本文最初发表于2016年12月 xff0c 现已更新以包含其他信息 在过去的几年中 xff0c 对民用 xff0c 军事和商用无人机的兴趣Swift增长 xff0c 这也带动了制造商社区对开源无人机项目的
  • 开源协议 自主发展_开源推动科学发展的9个故事

    开源协议 自主发展 如今 xff0c 科学可能看起来更像开源 世界各地的研究人员和科学家都在呼吁获得免费许可的数据集 开放获取发布条件 xff1b 以及协作 xff0c 透明的同行评审 他们正在寻找开放源代码原则可以增强数字时代知识生产实践
  • 开源 word 替代_5种Google文档的开源替代品

    开源 word 替代 每天处理大量文档时 xff0c 无论您写什么 xff08 白皮书 xff0c 手册 xff0c 演示文稿 xff0c 不同的市场营销材料 xff0c 合同等 xff09 xff0c 都必须在某个时候 xff08 最常见
  • vscode快捷键 & java/c++环境

    vscode快捷键 amp java c 43 43 环境 vscode快捷键环境配置javac 43 43 个人习惯设置参考 vscode快捷键 快捷键功能Ctrl 43 Shift 43 P 或 F1显示所有命令Ctrl 43 空格触发
  • IIC通信协议(简单易理解版)

    IIC通信协议简介 xff1a IIC xff08 也记为I2C xff0c 读作I 2C xff0c inter integrated Circuit集成电路总线 xff0c 最早是飞利浦在1982年开发设计并用于自己的芯片上 xff0c
  • linux防病毒软件_十大Linux最佳防病毒软件-Linux防病毒软件列表!

    linux防病毒软件 Today s article is all about the best Antivirus for Linux But if Linux is so secure why do we need to have an
  • Python isinstance()

    Python isinstance function is used to check if an object is an instance of the specified class or not Python的isinstance