编辑“︁
Python
”︁(章节)
跳转到导航
跳转到搜索
Template:Editnotice load/content
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
=== 类型 === [[File:Python 3.13 Standrd Type Hierarchy-en.svg|thumb|340px|Python 3的标准类型层级<ref>{{cite web|url=https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy|title=The Python Language Reference - 3. Data model - The standard type hierarchy}}</ref>]] Python使用[[鸭子类型]],并拥有有类型的对象,和无类型的变量名字。在[[编译期]]不检查类型约束,而宁愿在一个对象上的操作出现可能的失败,表现出这个给定对象不具有适合的类型。尽管是[[类型系统|动态类型]]系统,Python却是[[类型系统|强类型]]的,禁止没有明确定义的操作,比如将一个数和一个字符串相加,而不是默默的去尝试转换使其有意义。 Python有着范围广泛的基本数据类型。同时具备常规的整数和[[浮点数|浮点]]算术,它透明的支持[[高精度计算|任意精度算术]]、[[复数 (数学)|复数]]和{{le|十进制浮点|Decimal floating point|十进制浮点数}}。Python支持种类繁多的字符串操作。在Python中,字符串是[[不可变对象|不可变]]的,所以在其他编程语言中可能就地改变字符串的字符串操作,比如字符替换,在Python中返回新的字符串。 Python有一个非常有用特征,就是[[集合 (计算机科学)|搜集]](或称[[容器 (数据类型)|容器]])类型的概念。一般的说,搜集是以一种易于引用或索引的方式,包含其他对象的对象。Python的搜集类型包括了[[序列]]、[[映射]]和[[集合 (数学)|集合]],Python提供了广泛的搜集操纵能力,比如内建包含检查和通用[[迭代器]][[协议 (面向对象编程)|协议]]。 [[串列 (抽象资料型别)|列表]]([[动态数组]])、[[元组]]和[[字符串]]是序列类型。所有序列类型都有位置索引,并且除了字符串,都可以包含任意类型的对象,在同一个序列中可以包括多种类型的对象。字符串和元组是不可变的,使得它们成为字典的键的完美候选者。列表是可变的,元素可以被插入、删除、修改、添加或[[原地算法|就地]]排序。 [[关联数组|字典]]是无次序的映射类型,它将一组[[不可变对象|不可变]]的键,映射到相应的元素上。在字典中的键,必须是不可变的Python类型,比如整数或字符串,因为在底层它们是通过[[散列函数]]实现的。{{le|集合 (抽象数据类型)|Set (abstract data type)|集合}}是无次序的类型,它包含唯一性的不可变对象作为元素。有二种类型的集合:可变的<code>set</code>和不可变的<code>frozenset</code>。 Python允许编程者使用[[类 (计算机科学)|类]],定义自己的类型<ref name="unify" />。类的新[[对象 (计算机科学)|实例]],是通过调用这个类的[[构造器]]而创建的,而类型和类都是[[元类]]<code>type</code>的实例,元类<code>type</code>更是其自身的实例,这允许了[[元编程]]和[[反射式编程|反射]]。Python支持对类型和类的广泛[[类型内省|内省]],它们可以被读取和比较。{{efn|1=<nowiki /> 两个类及[[元类]]等的实例关系(蓝色连接)与继承关系(绿色连接)示意图: {{{!}} border=0 cellpadding=0 cellspacing=0 {{!}}- {{!}} {{!}}{{!}} style="width:4ex" {{!}} {{!}}{{!}} {{!}}- style="vertical-align:top" {{!}} <syntaxhighlight lang="python"> r = object c = type class M(c): pass class A(metaclass=M): pass class B(A): pass b = B() </syntaxhighlight> {{!}}{{!}} {{!}}{{!}}[[File:Eigenclass-model-sample-pr.svg|x240px]] {{!}}} <syntaxhighlight lang="pycon"> >>> type(b) <class '__main__.B'> >>> print(type(B), B.__bases__) <class '__main__.M'> (<class '__main__.A'>,) >>> print(type(A), A.__bases__) <class '__main__.M'> (<class 'object'>,) >>> print(type(M), M.__bases__) <class 'type'> (<class 'type'>,) >>> print(type(c), c.__bases__) <class 'type'> (<class 'object'>,) >>> print(type(r), r.__bases__) <class 'type'> () </syntaxhighlight>}} 长期规划是支持{{le|渐进类型|gradual typing}}<ref name="pep483">{{cite web|url=https://peps.python.org/pep-0483/|title=PEP 483 – The Theory of Type Hints|access-date=2023-03-16|archive-date=2023-03-26|archive-url=https://web.archive.org/web/20230326180923/https://peps.python.org/pep-0483/|dead-url=no}}</ref>,并且自从Python 3.5,语言的语法允许指定静态类型,但在缺省实现CPython中不检查它们<ref name="pep484" />。静态类型检查器mypy,支持编译期类型检查<ref name="mypy">{{cite web |url=http://mypy-lang.org/ |title=mypy - Optional Static Typing for Python |accessdate=2017-01-28 |archive-date=2020-06-06 |archive-url=https://web.archive.org/web/20200606192012/http://mypy-lang.org/ |dead-url=no }}</ref>。 {|class="wikitable" |+Python 3内建类型举要 |- ! 类型 ! style="width: 3em;" | [[不可变对象|可变性]] ! 描述 ! style="width: 20em;" | 语法例子 |- style="vertical-align:top;" | <code>bool</code> | 不可变 | [[布尔值]],有表示值<code>False</code>和<code>True</code>的两个对象。作为整数类型的子类型,除了在转换成字符串时转换为<code>"False"</code>和<code>"True"</code>之外,它们在几乎所有上下文中表现得如同<code>0</code>和<code>1</code>。 | {{code|lang=python|True}}<br>{{code|lang=python|False}} |- style="vertical-align:top;" | <code>int</code> | 不可变 | [[整数 (计算机科学)|整数]],其大小在理论上无限制,实际上受限于内存<ref name="pep0237">{{cite web |url=https://www.python.org/dev/peps/pep-0237/ |title=PEP 237 - Unifying Long Integers and Integers |author=Moshe Zadka, Guido van Rossum |date=2001-03-11 |archive-date=2020-05-28 |archive-url=https://web.archive.org/web/20200528063237/https://www.python.org/dev/peps/pep-0237/ |dead-url=no }}</ref>。 | {{code|lang=python|42}} |- style="vertical-align:top;" | <code>float</code> | 不可变 | [[双精度浮点数|双精度]][[浮点数]],确切精度依赖于机器。一般实现为[[IEEE 754]]标准[[双精度浮点数|binary64]]浮点数,它有53个[[二进制]][[有效数字|有效]][[数位 (数学)|数位]]精度<ref>{{Cite web|title=The Python Tutorial - 15. Floating Point Arithmetic: Issues and Limitations|url=https://docs.python.org/3/tutorial/floatingpoint.html|quote=almost all platforms map Python floats to IEEE-754 “double precision”.|access-date=2023-03-23|archive-date=2023-06-02|archive-url=https://web.archive.org/web/20230602024126/https://docs.python.org/3/tutorial/floatingpoint.html|dead-url=no}}</ref>。 |{{code|lang=python|1.414}} |- style="vertical-align:top;" | <code>complex</code> | 不可变 | [[複數 (數學)|複數]],即分别表示实部与虚部的两个双精度浮点数的[[有序对]]。 | {{code|lang=python|3+2.7j}} |- style="vertical-align:top;" | <code>range</code> | 不可变 | 数的序列,通常用在<code>for</code>循环中指定循环次数<ref>{{cite web |title=The Python Standard Library - Built-in Types - Ranges |url=https://docs.python.org/3/library/stdtypes.html#typesseq-range |accessdate=2019-10-03 |archive-date=2020-06-14 |archive-url=https://web.archive.org/web/20200614194325/https://docs.python.org/3/library/stdtypes.html#typesseq-range |dead-url=no }}</ref>。 | {{code|lang=python|range(1, 10)}}<br>{{code|lang=python|range(10, -5, -2)}} |- style="vertical-align:top;" | <code>str</code> | 不可变 | [[字符串]],即[[Unicode]][[代码点]]序列<ref>{{cite web|url=https://docs.python.org/3/howto/unicode.html|title=Python HOWTOs — Unicode HOWTO}}</ref>。其代码点都在区间<code>[\u0000, \uffff]</code>或<code>[\U00010000, \U0010ffff]</code>之内。 | {{code|lang=python|'Wiki' "pedia"}}<br /><syntaxhighlight lang="python"> """Spanning multiple lines"""</syntaxhighlight> |- style="vertical-align:top;" | <code>bytes</code> | 不可变 | rowspan="2" |[[字节]]序列,其项目是8位字节,可以用区间<code>[0x00, 0xff]</code>的整数表示。 | {{code|lang=python|b'Wiki' b"pedia"}}<br>{{code|lang=python|bytes([0x68, 0x65, 0x78])}} |- style="vertical-align:top;" | <code>bytearray</code> | 可变 | {{code|lang=python|bytearray(b'Wiki' b"pedia")}}<br>{{code|lang=python|bytearray([0x68, 0x65, 0x78])}} |- style="vertical-align:top;" | <code>list</code> | 可变 | [[串列 (抽象资料型别)|列表]],可以包含任意的Python对象。 | {{code|lang=python|[4.0, 'string', True]}}<br>{{code|lang=python|[]}} |- style="vertical-align:top;" | <code>tuple</code> | 不可变 | [[元组]],可以包含任意的Python对象。 | {{code|lang=python|(4.0, 'string', True)}}<br>{{code|lang=python|()}} |- style="vertical-align:top;" | <code>dict</code> | 可变 | [[关联数组|字典]]是[[键-值对]]的[[集合 (计算机科学)|搜集]]。键在其生存期间是不可变的(称其为可[[散列表|散列]]的),列表、字典或其它可变对象和包含它们的对象不可以用作键。 | {{code|lang=python|{'key1': 1.0, 3: False} }}<br>{{code|lang=python|{} }} |- style="vertical-align:top;" | <code>set</code> | 可变 | rowspan="2" |无序的{{le|集合 (抽象数据类型)|Set (abstract data type)|集合}},包含唯一性的不可变的对象,它们不能用任何下标来索引。 | {{code|lang=python|{4.0, 'string', True} }}<br>{{code|lang=python|set()}} |- style="vertical-align:top;" | <code>frozenset</code> | 不可变 | {{code|lang=python|frozenset([4.0, 'string', True])}} |- style="vertical-align:top;" | <code>types.EllipsisType</code> | 不可变 | 这个类型的值是有一个单一对象,它表示为{{le|文字 (计算机编程)|Literal (computer programming)|文字}}<code>...</code>或内建名字<code>[[省略号 (计算机编程)|Ellipsis]]</code>,它的[[真值]]为真。它用于[[NumPy]]多维阵列索引<ref>{{cite web|url=https://numpy.org/doc/stable/user/basics.indexing.html#dimensional-indexing-tools|title=NumPy fundamentals - Indexing on ndarrays - Dimensional indexing tools|access-date=2023-03-23|archive-date=2023-06-01|archive-url=https://web.archive.org/web/20230601013722/https://numpy.org/doc/stable/user/basics.indexing.html#dimensional-indexing-tools|dead-url=no}}</ref>。 | {{code|lang=python|...}}<br>{{code|lang=python|Ellipsis}} |- style="vertical-align:top;" | <code>types.NoneType</code> | 不可变 | 这个类型的值是一个单一对象<code>None</code>(相当于其他语言的<code>[[空指针|Null]]</code>)<ref>{{cite web|url=https://docs.python.org/3/library/stdtypes.html?highlight=null#the-null-object|title=The Python Standard Library - Built-in Types - The Null Object|access-date=2023-03-24|archive-date=2023-03-24|archive-url=https://web.archive.org/web/20230324024843/https://docs.python.org/3/library/stdtypes.html?highlight=null#the-null-object|dead-url=no}}</ref>,它被用来指示值的缺席,它的[[真值]]为假。 | {{code|lang=python|None}} |- style="vertical-align:top;" | <code>types.NotImplementedType</code> | 不可变 | 这个类型的值是一个单一对象<code>NotImplemented</code>,数值方法和丰富比较方法如果没有实现在所提供运算元上的这个运算则应当返回这个值,它不应该在[[布尔值]]上下文中求值。 | {{code|lang=python|NotImplemented}} |} 除了各种数据类型,Python解释器还内建了很多其他类型,包括可调用类型:用户定义函数、实例方法、生成器函数、协程函数、异步生成器函数、内建函数、内建方法、类、类方法;模块,定制类,类实例,I/O对象(也叫做文件对象),和暴露给用户的一些内部类型:代码对象、框架对象、溯回对象、切片对象、静态方法对象、类方法对象。
摘要:
请注意,所有对Local Chinese Wikipedia的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Project:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
大陆简体
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息