21.标准Type的分类
作者将标准Type分成三个Model,以搞清相互之间的关系,以及如何动作。
①Storage Model:
| Storage Model Category | Python Types That Fit Category |
| Scalar/atom | Numbers (all numeric types), strings (all are literals) |
| Container | Lists, tuples, dictionaries |
String不被看成Container是因为没有Character这个基本Type。
②Update Model:
| Update Model Category | Python Types That Fit Category |
| Mutable | Lists, dictionaries |
| Immutable | Numbers, strings, tuples |
Number和String也是Immutable,尽管如下语句:
x = 'Python numbers and strings'
x = 'are immutable?!? What gives?'
i = 0
i = i + 1
但其实x、i并非被更改了值,而是系统生成了新的对象,然后将x、i指向这个新的对象,原来的对象将被垃圾回收。通过查看id(x)可以看出其前后的差别。
③Access Model:
| Access Model Category | Python Types That Fit Category |
| Direct | Numbers |
| Sequence | Strings, lists, tuples |
| Mapping | Dictionaries |
综合:
|
Data-Type |
Storage-Model |
Update-Model |
Access-Model |
|
Numbers |
Scalar |
Immutable |
Direct |
|
Strings |
Scalar |
Immutable |
Sequence |
|
Lists |
Container |
Mutable |
Sequence |
|
Tuples |
Container |
Immutable |
Sequence |
|
Dictionaries |
Container |
Mutable |
Mapping |
本文从StorageModel、UpdateModel和AccessModel三个方面对Python的标准Type进行了详细分类,并解释了Number、String等类型的Immutable特性。
922

被折叠的 条评论
为什么被折叠?



