当前位置:网站首页>isinstance()函数用法
isinstance()函数用法
2022-06-26 08:30:00 【冯大少】
isinstance() 函数来判断一个对象是否是一个已知的类型,通常用于判断两个类型是否相同。

实例:
1. 当输入的 x < y 时,num 的 三元表达式 返回 x 的值 为 100,其类型通过 int 转换后,和 isinstance 的 classinfo params “int” 对比类型,因此得出 为 “ True"

2. 相反,当输入的 x < y 时,num 的 三元表达式 返回 " x < y " ,其类型为 “str”, 和 isinstance 的 classinfo params “int” 对比类型,因此得出 为 “ False"

3. 当输入的 x < y 时,num 的 三元表达式 返回 “False” , 和 isinstance 的 classinfo params “int” 对比类型,由于 False 也可以理解为等同于 0, 和 int 类型匹配,因此得出 为 “ True" 。

4. 当输入的 x < y 时,num 的 三元表达式 返回 “False” , 和 isinstance 的 classinfo params “str” 对比类型, 由于 False 是属于 bool 布尔类型, 好多时会不小心误解为 str。 因此得出 为 “ False " 。

5. 当输入的 x < y 时,num 的 三元表达式 返回 “False” , 和 isinstance 的 classinfo params “bool” 对比类型,因此得出 为 “ True" 。

注意: params里,如果classinfo类型为字符串,必须为 str, 如果写成 string, 会报错。

type() 与 isinstance()区别:
创建 Thursday 类 和 继承Thursday 的 Friday
print(isinstance(Thursday(),Thursday)) #输出结果为 True
print(type(Thursday()) == Thursday) #输出结果为 True
print(isinstance(Friday(),Thursday)) #输出结果为 True, Friday() 继承于 Thursday
print(type(Friday()) == Thursday) #输出结果为 False, type() 函数无法识别子类,父类的继承关系

边栏推荐
- Whale conference one-stop intelligent conference system helps organizers realize digital conference management
- Exploration of webots and ROS joint simulation (I): software installation
- Text to SQL model ----irnet
- MPC learning notes (I): push MPC formula manually
- 利用无线技术实现分散传感器信号远程集中控制
- Principle of playing card image segmentation
- [unity mirror] use of networkteam
- Analysis of Yolo series principle
- (1) Turn on the LED
- 多台三菱PLC如何实现无线以太网高速通讯?
猜你喜欢

Principle of playing card image segmentation

static const与static constexpr的类内数据成员初始化

Installation of jupyter

opencv学习笔记三

(2) Buzzer

Matlab drawing checkerboard (camera calibration)

Application of wireless charging receiving chip xs016 coffee mixing cup

Convex optimization of quadruped

51 MCU project design: Based on 51 MCU clock perpetual calendar

Design of reverse five times voltage amplifier circuit
随机推荐
How to correctly PIP install pyscipopt
Text to SQL model ----irnet
Using MySQL and Qt5 to develop takeout management system (I): environment configuration
Drawing with MATLAB (2) -- color ring
CodeBlocks integrated Objective-C development
多台三菱PLC如何实现无线以太网高速通讯?
I Summary Preface
GHUnit: Unit Testing Objective-C for the iPhone
Line detection_ nanyangjx
How to use leetcode
OpenCV Learning notes iii
Google Chrome collection
Trimming_ nanyangjx
Interpretation of x-vlm multimodal model
Use of PCL
Apple motherboard decoding chip, lightning Apple motherboard decoding I.C
(4) Independent key
1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
Parameter understanding of quad dataloader in yolov5
STM32 project design: smart door lock PCB and source code based on stm32f1 (4 unlocking methods)