Python 内建函数 - isinstance(object, classinfo)

Manual

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

直译

如果object参数是classinfo参数的一个实例或子类(直接、间接或虚拟)实例,返回True。如果object不是给定的类型的对象,函数返回False。如果classinfo是一个类型对象的元组(或递归等其他元组),且object是一个任意类型的实例,则返回True。如果classinfo不是类型或类型的元组,则会引发TypeError异常。

实例

>>> class 优快云:
    def __init__(self):
        print('Hello 优快云er!')
>>> isinstance(优快云(), 优快云)
Hello 优快云er!
True

>>> class 优快云er(优快云):
    def __init__(self):
        print('Hello world!')
>>> isinstance(优快云er(), 优快云)
Hello world!
True

>>> x = 1
>>> isinstance(x, 优快云)
False

拓展阅读

issubclass()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值