Python 有哪些重要的语言特性(习语)需要尽早学习[重复]

2024-05-05

我有兴趣了解 StackOverflow 社区认为 Python 的重要语言特性(习语)是什么。将程序员定义为 Pythonic 的特征。

Python (pythonic) 习语 - Python 语言自然的或特有的“代码表达式”。

另外,所有 Python 程序员都应该尽早学习哪些习惯用法?

提前致谢

Related:

  • 像 Pythonista 一样编码:惯用的 Python http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
  • Python:我错过了什么吗? https://stackoverflow.com/questions/566865/python-am-i-missing-something

Python 是一种语言,可以描述为:

“你可以适应的规则 你的手掌上有一个巨大的袋子 钩子”。

python 中的几乎所有内容都遵循相同的简单标准。一切都是可访问的、可改变的、可调整的。语言层面的元素很少。

以 len(data) 内置函数为例。len(data)只需检查一个即可工作data.__len__()方法,然后调用它并返回值。那样,len()可以作用于任何实现了__len__() method.


首先了解类型和基本语法:

  1. 动态强类型语言
  2. 布尔、整数、浮点数、字符串、列表、元组、字典、集合
  3. 陈述,缩进,“一切都是对象”
  4. 基本函数定义

然后继续学习 python 的工作原理:

  1. 导入和模块(非常简单)
  2. python 路径 (sys.path)
  3. the dir()功能
  4. __builtins__

一旦您了解了如何将各个部分组合在一起,请返回并涵盖一些更高级的语言功能:

  1. 迭代器
  2. 覆盖像__len__(有很多这样的)
  3. 列表推导式和生成器
  4. 类和对象(同样,一旦您了解了一些规则,就非常简单)
  5. python继承规则

一旦您对这些项目有了一定程度的熟悉(重点关注是什么让它们变得Python化),请查看更具体的项目:

  1. python 中的线程(注意全局解释器锁)
  2. 上下文管理器
  3. 数据库访问
  4. file IO
  5. sockets
  6. etc...

并且永远不会忘记Python 之禅(蒂姆·彼得斯)

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 有哪些重要的语言特性(习语)需要尽早学习[重复] 的相关文章

随机推荐