互联网技术 / 互联网资讯 · 2024年1月16日

Python内置函数-isinstance() 的云计算开发指南

描述

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

isinstance() 与 type() 区别:

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

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

如果要判断两个类型是否相同推荐使用 isinstance()。

语法

以下是 isinstance() 方法的语法:

isinstance(object, claSSinfo)

参数

object — 实例对象。

claSSinfo — 可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值

如果对象的类型与参数二的类型(claSSinfo)相同则返回 TRue,否则返回 FAlse。

实例

以下展示了使用 isinstance() 方法的实例:

云计算开发:Python内置函数-isinstance() 函数详解

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册