这篇文章主要用来记录跟 Python 语法相关的博客,方便笔者日后查阅。
- 增加摘要
- 丰富内容
- 分门别类
with
Context Managers and the “with” Statement in Python – Dan Bader
文件处理
Working with Files in Python – Vuyisile Ndlovu
内存管理
Memory Management in Python – Alexander VanTol
生成器
Generator Expressions in Python: An Introduction – Dan Bader
全局解释所(GIL)
What Is the Python Global Interpreter Lock (GIL)? – Abhinav Ajitsaria
关于 if __name__ == '__main__'
的解释
What does if name == “main”: do? – stackoverflow
Python 中的 if name == ‘main’ 该如何理解
OrderedDict 和 Dict 的区别
摘要
过去的很多年间,Python 中的dict
是无序的数据结构,也就是说数据插入的顺序不会被存储的,所以想要按照插入顺序遍历字典往往需要借助其他的工具(比如列表)。针对这个问题,Python 的开发者们提出了OrderedDict
,一种按照键插入顺序来保存键值对的字典(在Python 3.1 提出)。但是,在Python 3.6 之后,
dict
变成了一个有序的数据结构,换句话说OrderedDict
要解决的问题好像不存在了。
那么我们还需要使用OrderedDict
吗?下文就探讨了这个问题。