本文主要是介绍《Core Python 2nd》读书摘要二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 |
这篇关于《Core Python 2nd》读书摘要二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!