news 2026/6/26 4:26:08

Python 开发 - type 函数(type 函数的基本使用、type 函数的返回值、type 函数与 isinstance 函数、type 函数元编程)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python 开发 - type 函数(type 函数的基本使用、type 函数的返回值、type 函数与 isinstance 函数、type 函数元编程)

一、type 函数的基本使用

  1. 检查基本类型
print(type(123))print(type(3.14))print(type("hello"))print(type([1,2,3]))print(type({"a":1}))
# 输出结果 <class 'int'> <class 'float'> <class 'str'> <class 'list'> <class 'dict'>
  1. 检查自定义类型
classMyClass:passobj=MyClass()print(type(obj))
# 输出结果 <class '__main__.MyClass'>

二、type 函数的返回值

  1. type 函数的返回值是一个类型对象
result=type(123)print(result)print(type(result))
# 输出结果 <class 'int'> <class 'type'>
  1. 使用__name__属性获取类型名称
result=type(123)print(result)print(result.__name__)
# 输出结果 <class 'int'> int

三、type 函数与 isinstance 函数

  1. 使用 type 函数进行类型检查
defcheck_type(obj):iftype(obj)==int:return"int"eliftype(obj)==float:return"float"eliftype(obj)==str:return"str"eliftype(obj)==list:return"list"eliftype(obj)==dict:return"dict"else:return"unknown"print(check_type(123))print(check_type(3.14))print(check_type("hello"))print(check_type([1,2,3]))print(check_type({"a":1}))
# 输出结果 int float str list dict
  1. type 函数不能判断类型的继承关系,更推荐使用 isinstance 函数进行类型检查
classParent:passclassChild(Parent):passobj=Child()print(type(obj)==Child)print(type(obj)==Parent)print(isinstance(obj,Child))print(isinstance(obj,Parent))
# 输出结果 True False True True

四、type 函数元编程

  1. 动态创建类
MyClass=type('MyClass',(),{'x':42})obj=MyClass()print(obj.x)
# 输出结果 42
  1. 动态创建类,继承现有类
classBase:defshow(self):return"base class"Child=type('Child',(Base,),{'value':100})c=Child()print(c.show())print(c.value)
# 输出结果 base class 100
  1. 动态创建类,带方法
defsay_hello(self):returnf"hello{self.name}"Person=type('Person',(),{'__init__':lambdaself,name:setattr(self,'name',name),'greet':say_hello})p=Person("tom")print(p.greet())
# 输出结果 hello tom
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/24 19:57:26

Java 多线程编程 - 线程池 awaitTermination 方法

awaitTermination 方法 1、基本介绍 boolean awaitTermination(long timeout, TimeUnit unit)throws InterruptedException;参数类型说明timeoutlong等待时间unitTimeUnit时间单位 返回值说明true线程池在超时前已终止false超时后线程池仍未终止awaitTermination 是 Java 线…

作者头像 李华
网站建设 2026/6/24 18:53:52

FaceFusion更新日志追踪:每月都有新功能上线

AI换脸技术的边界与工程伦理&#xff1a;为何专业分工不可逾越在人工智能技术迅猛发展的今天&#xff0c;我们时常看到各类AI工具以前所未有的速度迭代更新——FaceFusion每月上线新功能、DeepNude类项目引发伦理争议、Stable Diffusion开放模型催生创作革命。这些现象背后&…

作者头像 李华
网站建设 2026/6/25 22:53:25

(Open-AutoGLM实战白皮书)首次公开:跨平台任务调度的7种高效模式

第一章&#xff1a;Open-AutoGLM 跨应用任务处理竞品分析在跨应用自动化任务处理领域&#xff0c;多个框架和平台已展现出不同的技术路径与能力边界。Open-AutoGLM 作为新兴的开源解决方案&#xff0c;其核心优势在于结合大语言模型&#xff08;LLM&#xff09;驱动的任务解析与…

作者头像 李华
网站建设 2026/6/24 22:17:35

分布式幂等性:30字讲透核心要点

幂等性处理是分布式系统和微服务架构中保证数据一致性与系统健壮性的核心概念。我们来系统性地梳理一下。一、什么是幂等性&#xff1f;定义&#xff1a;一个操作&#xff08;或接口&#xff09;被重复执行多次所产生的效果&#xff0c;与仅执行一次所产生的效果完全相同。核心…

作者头像 李华
网站建设 2026/6/24 17:27:28

FaceFusion能否对接OneDrive?微软生态无缝衔接

FaceFusion 与 OneDrive 的无缝集成&#xff1a;打通 AI 生成与办公生态的“最后一公里”在内容创作日益依赖人工智能的今天&#xff0c;一个现实问题摆在开发者和企业面前&#xff1a;我们如何让 AI 工具产出的结果&#xff0c;不再沉睡于本地磁盘&#xff0c;而是自动进入用户…

作者头像 李华
网站建设 2026/6/26 1:32:33

【AI模型部署必读】:Open-AutoGLM云端推理速度提升3倍的秘密路径

第一章&#xff1a;Open-AutoGLM 端侧 vs 云端部署性能权衡在边缘计算与云计算并行发展的背景下&#xff0c;Open-AutoGLM 的部署策略需在端侧与云端之间做出性能与效率的权衡。端侧部署能够显著降低推理延迟、保障数据隐私&#xff0c;并减少对网络带宽的依赖&#xff1b;而云…

作者头像 李华