hutool.core.util.classes

Module Contents

Classes

ClassUtil

类工具类

API

class hutool.core.util.classes.ClassUtil

类工具类

提供对Python类和对象的常用操作方法,包括获取类名、模块名、类型判断等。

static get_class_name(obj: Any, is_simple: bool = True) str

获取类名

参数:
  • obj – 对象或类

  • is_simple – 是否返回简单类名(不含模块路径),默认True

返回:

类名字符串

static get_package_name(obj: Any) str

获取模块名

参数:

obj – 对象或类

返回:

模块名字符串

static is_basic_type(obj: Any) bool

是否为基本类型

基本类型包括:int, float, str, bool, bytes, complex, type(None)

参数:

obj – 待检查的对象

返回:

是否为基本类型

static is_instance(obj: Any, class_or_tuple: Any) bool

是否为指定类型的实例

参数:
  • obj – 待检查的对象

  • class_or_tuple – 类或类的元组

返回:

是否为指定类型的实例

static get_public_fields(obj: Any) List[str]

获取公开字段

返回对象中不以单下划线开头的属性名称列表。

参数:

obj – 对象

返回:

公开字段名列表

static get_methods(obj: Any) List[str]

获取方法列表

返回对象中不以单下划线开头的方法名称列表。

参数:

obj – 对象或类

返回:

方法名列表

static has_method(obj: Any, method_name: str) bool

是否有指定方法

参数:
  • obj – 对象或类

  • method_name – 方法名

返回:

是否存在该方法

static create_instance(class_path: str) Any

通过完整类路径创建实例

支持模块级函数和类实例化两种方式。 例如: ‘json.loads’, ‘datetime.datetime’, ‘collections.OrderedDict’

参数:

class_path – 完整类路径或函数路径,如 ‘datetime.datetime’

返回:

创建的实例或返回的函数结果

抛出:
  • ImportError – 模块导入失败

  • AttributeError – 属性不存在

static is_subclass(cls: Type, parent_cls: Type) bool

是否为子类

参数:
  • cls – 待检查的类

  • parent_cls – 父类

返回:

是否为父类的子类