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

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

快速链接到答案:

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


当前回答

只需少量的工作,线程模块就变得非常容易使用。此装饰器更改函数,使其在自己的线程中运行,返回占位符类实例,而不是常规结果。你可以通过检查placeolder来探测答案。结果或通过调用placeholder.awaitResult()来等待它。

def threadify(function):
    """
    exceptionally simple threading decorator. Just:
    >>> @threadify
    ... def longOperation(result):
    ...     time.sleep(3)
    ...     return result
    >>> A= longOperation("A has finished")
    >>> B= longOperation("B has finished")

    A doesn't have a result yet:
    >>> print A.result
    None

    until we wait for it:
    >>> print A.awaitResult()
    A has finished

    we could also wait manually - half a second more should be enough for B:
    >>> time.sleep(0.5); print B.result
    B has finished
    """
    class thr (threading.Thread,object):
        def __init__(self, *args, **kwargs):
            threading.Thread.__init__ ( self )  
            self.args, self.kwargs = args, kwargs
            self.result = None
            self.start()
        def awaitResult(self):
            self.join()
            return self.result        
        def run(self):
            self.result=function(*self.args, **self.kwargs)
    return thr

其他回答

创建枚举

在Python中,你可以这样做来快速创建一个枚举:

>>> FOO, BAR, BAZ = range(3)
>>> FOO
0

但是“枚举”不一定是整数值。你甚至可以这样做:

class Colors(object):
    RED, GREEN, BLUE, YELLOW = (255,0,0), (0,255,0), (0,0,255), (0,255,255)

#now Colors.RED is a 3-tuple that returns the 24-bit 8bpp RGB 
#value for saturated red

标准Python中的垃圾邮件模块

它用于测试目的。

我选自ctypes教程。自己试试吧:

>>> import __hello__
Hello world...
>>> type(__hello__)
<type 'module'>
>>> from __phello__ import spam
Hello world...
Hello world...
>>> type(spam)
<type 'module'>
>>> help(spam)
Help on module __phello__.spam in __phello__:

NAME
    __phello__.spam

FILE
    c:\python26\<frozen>

命名格式

% -formatting接受字典(也应用%i/%s等验证)。

>>> print "The %(foo)s is %(bar)i." % {'foo': 'answer', 'bar':42}
The answer is 42.

>>> foo, bar = 'question', 123

>>> print "The %(foo)s is %(bar)i." % locals()
The question is 123.

由于locals()也是一个字典,您可以简单地将其作为字典传递,并从局部变量中获得% -替换。我认为这是不受欢迎的,但简化了事情。

新的样式格式

>>> print("The {foo} is {bar}".format(foo='answer', bar=42))

操纵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完全导致下一次导入对该模块进行正常搜索,即使之前已经导入了该模块。

链接比较操作符:

>>> x = 5
>>> 1 < x < 10
True
>>> 10 < x < 20 
False
>>> x < 10 < x*10 < 100
True
>>> 10 > x <= 9
True
>>> 5 == x > 4
True

如果你认为它在做1 < x,结果是True,然后比较True < 10,这也是True,那么不,这真的不是发生的事情(见最后一个例子)。它实际上转化为1 < x和x < 10,以及x < 10和10 < x*10和x*10 < 100,但是类型更少,每个项只计算一次。