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

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

The isinstance() function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).

The syntax of isinstance() is:

isinstance(object, classinfo)

  

isinstance() Parameters

 The isinstance() takes two parameters:

  • object - object to be checked
  • classinfo - class, type, or tuple of classes and types

Return Value from isinstance()

The isinstance() returns:

  • True if the object is an instance or subclass of a class, or any element of the tuple
  • False otherwise

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


Example 1: How isinstance() works?

class Foo:  a = 5  fooInstance = Foo()print(isinstance(fooInstance, Foo))print(isinstance(fooInstance, (list, tuple)))print(isinstance(fooInstance, (list, tuple, Foo))) When you run the program, the output will be:
TrueFalseTrue

  

Example 2: Working of isinstance() with Native Types

 

numbers = [1, 2, 3]result = isinstance(numbers, list)print(numbers,'instance of list?', result)result = isinstance(numbers, dict)print(numbers,'instance of dict?', result)result = isinstance(numbers, (dict, list))print(numbers,'instance of dict or list?', result)number = 5result = isinstance(number, list)print(number,'instance of list?', result)result = isinstance(number, int)print(number,'instance of int?', result) When you run the program, the output will be:
[1, 2, 3] instance of list? True[1, 2, 3] instance of dict? False[1, 2, 3] instance of dict or list? True5 instance of list? False5 instance of int? True

 

转载于:https://www.cnblogs.com/Xingtxx/p/11046070.html

你可能感兴趣的文章
Javascript Ajax 异步请求
查看>>
DBCP连接池
查看>>
cannot run programing "db2"
查看>>
mysql做主从relay-log问题
查看>>
Docker镜像与容器命令
查看>>
批量删除oracle中以相同类型字母开头的表
查看>>
Java基础学习总结(4)——对象转型
查看>>
BZOJ3239Discrete Logging——BSGS
查看>>
SpringMVC权限管理
查看>>
spring 整合 redis 配置
查看>>
cacti分组发飞信模块开发
查看>>
浅析LUA中游戏脚本语言之魔兽世界
查看>>
飞翔的秘密
查看>>
Red Hat 安装源包出错 Package xxx.rpm is not signed
查看>>
编译安装mysql-5.6.16.tar.gz
查看>>
活在当下
查看>>
每天进步一点----- MediaPlayer
查看>>
PowerDesigner中CDM和PDM如何定义外键关系
查看>>
跨域-学习笔记
查看>>
the assignment of reading paper
查看>>