Python编程语言中有哪些鲜为人知但很有用的特性?

尽量将答案限制在Python核心。 每个回答一个特征。 给出一个例子和功能的简短描述,而不仅仅是文档链接。 使用标题作为第一行标记该特性。

快速链接到答案:

参数解包 牙套 链接比较运算符 修饰符 可变默认参数的陷阱/危险 描述符 字典默认的.get值 所以测试 省略切片语法 枚举 其他/ 函数作为iter()参数 生成器表达式 导入该 就地值交换 步进列表 __missing__物品 多行正则表达式 命名字符串格式化 嵌套的列表/生成器推导 运行时的新类型 .pth文件 ROT13编码 正则表达式调试 发送到发电机 交互式解释器中的制表符补全 三元表达式 试着/ / else除外 拆包+打印()函数 与声明


当前回答

操纵sys.modules

你可以直接操作模块缓存,使模块可用或不可用,如你所愿:

>>> import sys
>>> import ham
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named ham

# Make the 'ham' module available -- as a non-module object even!
>>> sys.modules['ham'] = 'ham, eggs, saussages and spam.'
>>> import ham
>>> ham
'ham, eggs, saussages and spam.'

# Now remove it again.
>>> sys.modules['ham'] = None
>>> import ham
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named ham

这甚至适用于可用的模块,在某种程度上也适用于已经导入的模块:

>>> import os
# Stop future imports of 'os'.
>>> sys.modules['os'] = None
>>> import os
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named os
# Our old imported module is still available.
>>> os
<module 'os' from '/usr/lib/python2.5/os.pyc'>

如最后一行所示,更改sys.;模块只影响将来的导入语句,而不影响过去的导入语句,所以如果你想影响其他模块,在给它们尝试导入模块的机会之前进行这些更改是很重要的——通常是在导入它们之前。None是sys中的一个特殊值。模块,用于负缓存(表明该模块第一次没有找到,因此没有必要再次查找)。任何其他值都将是导入操作的结果——即使它不是模块对象。您可以使用它将模块替换为与您想要的行为完全一致的对象。删除sys. exe表项。Modules完全导致下一次导入对该模块进行正常搜索,即使之前已经导入了该模块。

其他回答

在列表推导式中交错if和for

>>> [(x, y) for x in range(4) if x % 2 == 1 for y in range(4)]
[(1, 0), (1, 1), (1, 2), (1, 3), (3, 0), (3, 1), (3, 2), (3, 3)]

直到我学了哈斯克尔,我才意识到这一点。

Metaclasses

Python中的元类是什么?

特殊的方法

绝对的权力!

模块操作符中的Getter函数

模块操作符中的函数attrgetter()和itemgetter()可用于生成用于排序和搜索对象和字典的快速访问函数

Python库文档中的第6.7章

小心可变默认参数

>>> def foo(x=[]):
...     x.append(1)
...     print x
... 
>>> foo()
[1]
>>> foo()
[1, 1]
>>> foo()
[1, 1, 1]

相反,你应该使用一个表示“not given”的哨兵值,并将其替换为你想要的默认值:

>>> def foo(x=None):
...     if x is None:
...         x = []
...     x.append(1)
...     print x
>>> foo()
[1]
>>> foo()
[1]