news 2026/10/15 16:34:20

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/10/14 13:48:49

【收藏学习】大模型进阶之路:AI Agent架构设计与实践指南

AI Agent是大模型的进阶形态&#xff0c;通过自主决策、持续学习、多模态交互、工具集成和多智能体协作等五大特征&#xff0c;实现从"回答问题"到"完成目标"的转变。其技术架构包括感知、决策、执行、记忆和反馈优化六大模块&#xff0c;涵盖四种工作模式…

作者头像 李华
网站建设 2026/10/15 18:42:26

Open-AutoGLM部署怎么选?:端侧低延迟 vs 云端高算力,谁更胜一筹

第一章&#xff1a;Open-AutoGLM 端侧 vs 云端部署性能权衡在边缘计算与云计算并行发展的背景下&#xff0c;Open-AutoGLM 的部署策略面临端侧与云端之间的性能权衡。选择部署位置不仅影响推理延迟和资源消耗&#xff0c;还直接关系到用户体验与系统可扩展性。部署模式对比 端侧…

作者头像 李华
网站建设 2026/10/14 22:49:56

分布式幂等--银弹

“银弹”是一个在技术领域&#xff08;特别是软件工程&#xff09;非常经典且常用的比喻。一、字面与比喻含义字面意思&#xff1a;指传说中能杀死狼人、吸血鬼等超自然生物的子弹&#xff0c;通常由白银制成。它代表了能一举解决某个可怕难题的 “终极武器”。在技术和工程领域…

作者头像 李华
网站建设 2026/10/15 6:04:41

Open-AutoGLM上云还是留端?:90%开发者忽略的3个关键决策因素

第一章&#xff1a;Open-AutoGLM上云还是留端&#xff1f;——决策的底层逻辑在部署 Open-AutoGLM 这类大语言模型时&#xff0c;核心问题并非“能否运行”&#xff0c;而是“在哪里运行更优”。选择上云或本地部署&#xff0c;本质是性能、成本、安全与灵活性之间的权衡。性能…

作者头像 李华
网站建设 2026/10/14 23:02:57

FaceFusion支持Windows/Linux/MacOS全系统运行

FaceFusion跨平台部署的技术解析 在AI生成技术迅速普及的今天&#xff0c;一个工具能否“拿起来就用”&#xff0c;往往比它的算法精度更影响实际落地。FaceFusion 作为开源社区中人气颇高的换脸工具&#xff0c;近年来之所以能从小众实验项目走向广泛使用&#xff0c;关键就在…

作者头像 李华