理解 Python type() 与 isinstance():实战示例详解
理解 Python type() 与 isinstance():实战示例详解
什么是 Python type()?
Python 内置 type() 函数帮助你找到给定变量的类类型。例如输入字符串输出 <class 'str'>,列表输出 <class 'list'> 等。
使用 type() 可传单个参数,返回该参数的类类型,如 type(object)。
也可传三个参数 type(name, bases, dict),此时返回新的类型对象。
本教程你将学到:
- 什么是 Python type()?
- type() 语法
- type() 示例
- 示例:对类对象使用 type()
- 示例:在 type() 中使用 name、bases 和 dict
- 什么是 Python isinstance()?
- isinstance() 语法
- isinstance() 示例
- Python 中 type() 与 isinstance() 的区别
type() 语法
type() 有两种用法:
type(object) type(name, bases, dict)
参数:type(object)
- object:必需参数。若仅传此参数,返回该参数的类型。
参数:type(name, bases, dict)
- name:类名。
- bases:(可选)基类。
- dict:(可选)包含类定义的命名空间字典。
返回值:
若仅传 object,返回对象类型。
若传 type(name, bases, dict),返回新的类型对象。
type() 示例
示例中包含字符串、数字、浮点数、复数、列表、元组、字典和集合。用 type 查看各变量类型。
str_list = "Welcome to Guru99"
age = 50
pi = 3.14
c_num = 3j+10
my_list = ["A", "B", "C", "D"]
my_tuple = ("A", "B", "C", "D")
my_dict = {"A":"a", "B":"b", "C":"c", "D":"d"}
my_set = {'A', 'B', 'C', 'D'}
print("The type is : ", type(str_list))
print("The type is : ", type(age))
print("The type is : ", type(pi))
print("The type is : ", type(c_num))
print("The type is : ", type(my_list))
print("The type is : ", type(my_tuple))
print("The type is : ", type(my_dict))
print("The type is : ", type(my_set))
输出:
The type is :The type is : The type is : The type is : The type is : The type is : The type is : The type is :
示例:对类对象使用 type()
用 type() 检查类创建的对象时,返回类类型及类名。本例创建类并检查其对象类型。
class test:
s = 'testing'
t = test()
print(type(t))
输出:
示例:在 type() 中使用 name、bases 和 dict
type 也可用 type(name, bases, dict) 语法调用。
传给 type() 的三参数即类定义的三要素:name 为类名,bases 为基类,dict 为基类属性字典。
本例使用全部三参数。
class MyClass:
x = 'Hello World'
y = 50
t1 = type('NewClass', (MyClass,), dict(x='Hello World', y=50))
print(type(t1))
print(vars(t1))
输出:
{'x': 'Hello World', 'y': 50, '__module__': '__main__', '__doc__': None}
传三参数给 type() 可用基类属性初始化新类。
什么是 Python isinstance()?
Python isinstance 是内置函数。isinstance() 接受两参数,若第一参数是第二参数给定类信息的实例则返回真。
isinstance() 语法
isinstance(object, classtype)
参数
- object:要与 classtype 比较的对象。类型匹配返回真,否则假。
- class type:类型、类或类型/类的元组。
返回值:
对象是 classtype 实例返回真,否则假。
isinstance() 示例
示例:isinstance() 整数检查
age = isinstance(51, int)
print("age is an integer:", age)
输出:
age is an integer: True
示例:isinstance() 浮点数检查
pi = isinstance(3.14, float)
print("pi is a float:", pi)
输出:
pi is a float: True
示例:isinstance() 字符串检查
message = isinstance("Hello World", str)
print("message is a string:", message)
输出:
message is a string: True
示例:isinstance() 元组检查
my_tuple = isinstance((1,2,3,4,5), tuple)
print("my_tuple is a tuple:", my_tuple)
输出:
my_tuple is a tuple: True
示例:isinstance() 集合检查
my_set = isinstance({1,2,3,4,5}, set)
print("my_set is a set:", my_set)
输出:
my_set is a set: True
示例:isinstance() 列表检查
my_list = isinstance([1,2,3,4,5], list)
print("my_list is a list:", my_list)
输出:
my_list is a list: True
示例:isinstance() 字典检查
my_dict = isinstance({"A":"a", "B":"b", "C":"c", "D":"d"}, dict)
print("my_dict is a dict:", my_dict)
输出:
my_dict is a dict: True
示例:isinstance() 检查类
class MyClass:
_message = "Hello World"
_class = MyClass()
print("_class is a instance of MyClass() : ", isinstance(_class, MyClass))
输出:
_class is a instance of MyClass() True
Python 中 type() 与 isinstance() 的区别
| type() | isinstance() |
|---|---|
| 内置函数,帮助找到输入变量的类类型。 | 内置函数,比较值与给定类型。匹配返回真否则假。 |
| 返回类型对象 | 返回布尔值即真或假。 |
class A:
my_listA = [1,2,3]
class B(A):
my_listB = [1,2,3]
print(type(A()) == A)
print(type(B()) == A)
输出: True False type 子类检查返回假。 |
class A:
my_listA = [1,2,3]
class B(A):
my_listB = [1,2,3]
print(isinstance(A(), A))
print(isinstance(B(), A))
输出: True True isinstance() 检查子类返回真值。 |
总结
- Python 内置 type() 找到输入变量的类类型。如字符串输出
<class 'str'>,列表输出<class 'list'>。 - type() 可传单参数,返回该参数的类类型,如
type(object)。 - 也可传三参数
type(name, bases, dict),返回新类型对象。 - Python 内置 isinstance() 比较值与给定类型。匹配返回真否则假。可测试字符串、浮点数、整数、列表、元组、字典、集合、类等。
- isinstance() 方法可测试字符串、浮点数、整数、列表、元组、字典、集合、类等。
Python
- 树莓派与Python构建机器人:完整指南
- C# 表达式、语句与代码块:全面指南与实战示例
- 精通Python类型转换与强制转换:综合指南
- 精通 Python 数字、类型转换与数学运算
- Python print() 函数完全指南:基础用法、空行打印与自定义结束符
- Python str.count() 完全指南:统计字符与子串出现次数实战
- Python format() 字符串格式化完全指南:占位符、格式化规范、类与字典应用
- Python 字符串 find() 方法完全指南:语法、参数、替代方案与计数技巧
- 精通 Python Lambda 函数:实用示例与最佳实践
- Python round() 函数详解与实战示例
- 精通 Python map() 函数:语法、示例与最佳实践
- Python timeit():精准测量代码执行时间与实战示例