Python 中检查对象是否是类的实例
使用 isinstance()
函数检查对象是否是类的实例。 isinstance()
函数可以传递一个对象和一个或多个类,如果该对象是至少一个所提供类的实例,则返回 True。
class Employee():
pass
emp1 = Employee()
# ✅ 检查对象是否是特定类的实例
if isinstance(emp1, Employee):
# 👇️ this runs
print('The object is an instance of the class')
else:
print('The object is NOT an instance of the class')
# -----------------------------------
# ✅ 检查对象是否是多个类之一的实例
if isinstance(emp1, (str, list, int, Employee)):
print('The object is an instance of one of the classes')
else:
print('The object is NOT an instance of one of the classes')
如果传入的对象是传入类的实例或子类,则 isinstance
函数返回 True。
class Employee():
pass
emp1 = Employee()
print(isinstance(emp1, Employee)) # 👉️ True
print(isinstance(int, Employee)) # 👉️ False
isinstance()
函数也可以传递一个类元组。
如果对象是任何指定类的实例,则该函数返回 True。
class Employee():
pass
emp1 = Employee()
print(
isinstance(emp1, (str, list, int, Employee))
) # 👉️ True
print(
isinstance(emp1, (str, list, int))
) # 👉️ False
如果我们需要检查一个对象是否是任何类的实例,请检查该对象是否具有
__class__
属性。
class Employee():
pass
emp1 = Employee()
print(hasattr(emp1, '__class__')) # 👉️ True
print(hasattr(int, '__class__')) # 👉️ True
每个对象都有一个类,可以使用对象的 __class__
属性访问该类。
class Employee():
pass
emp1 = Employee()
print(emp1.__class__) # 👉️ <class '__main__.Employee'>
print(int.__class__) # 👉️ <class 'type'>
如果对象没有 __class__
属性,则它不会实例化任何类。
如果我们需要检查一个对象是否是一个类,请使用 inspect.isclass()
方法。
from inspect import isclass
class Employee():
pass
emp1 = Employee()
print(isclass(Employee)) # 👉️ True
print(isclass(emp1)) # 👉️ False
如果提供的对象是类,则 inspect.isclass
方法返回 True。
对于内置类和用户定义类,该方法返回 True。
检查变量是否是 Python 列表中任何类的实例
检查变量是否是列表中任何类的实例:
-
使用
tuple()
类将列表转换为元组。 -
使用
isinstance()
函数检查变量是否是任何类的实例。 -
如果满足条件,
instance()
函数将返回 True。
class Employee():
pass
a_list = [int, float, str, Employee]
emp1 = Employee()
if isinstance(emp1, tuple(a_list)):
# 👇️ this runs
print('The variable is an instance of one of the classes in the list')
else:
print('The variable is NOT an instance of any of the classes in the list')
# 👇️ True
print(
isinstance(
emp1,
(int, float, str, Employee)
)
)
我们使用 tuple()
类将类列表转换为元组。
如果传入的对象是传入类的实例或子类,则 isinstance
函数返回 True。
class Employee():
pass
emp1 = Employee()
print(isinstance(emp1, Employee)) # 👉️ True
print(isinstance(float, Employee)) # 👉️ False
isinstance()
函数也可以传递一个类元组。
如果对象是任何指定类的实例,则该函数返回 True。
class Employee():
pass
emp1 = Employee()
# 👇️ True
print(
isinstance(
emp1,
(int, float, str, Employee)
)
)
# 👇️ False
print(
isinstance(
emp1,
(int, float, str)
)
)
如果变量不是元组中任何类的实例,则返回 False。
如果我们将类存储在列表中,请使用 tuple()
类将列表转换为元组。
class Employee():
pass
a_list = [int, float, str, Employee]
emp1 = Employee()
# 👇️ True
print(
isinstance(
emp1,
tuple(a_list)
)
)
元组类接受一个可迭代对象并返回一个 tuple
对象。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。