博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python isinstance()
阅读量:2535 次
发布时间:2019-05-11

本文共 3214 字,大约阅读时间需要 10 分钟。

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 of . 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 , such as , tuple, , , etc.

让我们看一下具有内置的isinstance()函数的一些简单示例,例如 ,元组, , , 等。

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

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

Output:

输出:

i is int: Truef 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 with multiple classes.

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

class Person:    name = ''class Employee(Person):    id = 0p = 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: Truep isinstance of Employee: Falsee isinstance of Person: Truee 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: Truee 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()是一种实用程序函数,用于检查对象是否为特定类型。 我们可以使用它来执行类型检查并根据对象的类型执行操作。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://mtmzd.baihongyu.com/

你可能感兴趣的文章
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>