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

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

快速链接到答案:

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


当前回答

在运行时更改函数标签:

>>> class foo:
...   def normal_call(self): print "normal_call"
...   def call(self): 
...     print "first_call"
...     self.call = self.normal_call

>>> y = foo()
>>> y.call()
first_call
>>> y.call()
normal_call
>>> y.call()
normal_call
...

其他回答

>>> float('infinity')
inf
>>> float('NaN')
nan

更多信息:

http://docs.python.org/library/functions.html#float http://www.python.org/dev/peps/pep-0754/ Python nan和inf值

commands.getoutput

如果你想获得直接输出到stdout或stderr的函数的输出,就像os的情况一样。系统命令。Getoutput来拯救。整个模块都非常棒。

>>> print commands.getoutput('ls')
myFile1.txt    myFile2.txt    myFile3.txt    myFile4.txt    myFile5.txt
myFile6.txt    myFile7.txt    myFile8.txt    myFile9.txt    myFile10.txt
myFile11.txt   myFile12.txt   myFile13.txt   myFile14.txt   module.py

动态添加的属性

如果您想通过调用来向类添加一些属性,这可能会很有用。这可以通过重写__getattribute__成员函数来实现,该成员函数在使用点操作数时被调用。让我们看一个虚拟类为例:

class Dummy(object):
    def __getattribute__(self, name):
        f = lambda: 'Hello with %s'%name
        return f

当你实例化一个Dummy对象并进行方法调用时,你会得到以下结果:

>>> d = Dummy()
>>> d.b()
'Hello with b'

最后,您甚至可以为您的类设置属性,以便动态定义它。如果你使用Python web框架,想通过解析属性名来进行查询,这可能很有用。

我在github上有一个要点,这个简单的代码和一个朋友在Ruby上做的等效代码。

保重!

Getattr接受第三个参数

Getattr (obj, attribute_name, default)是这样的:

try:
    return obj.attribute
except AttributeError:
    return default

只不过attribute_name可以是任何字符串。

这对于鸭子输入非常有用。也许你有这样的东西:

class MyThing:
    pass
class MyOtherThing:
    pass
if isinstance(obj, (MyThing, MyOtherThing)):
    process(obj)

(顺便说一下,isinstance(obj, (a,b))表示isinstance(obj, a)或isinstance(obj, b)。)

当你创建一种新的东西时,你需要把它添加到它出现的任何地方。(这种结构在重新加载模块或以两个名称导入同一个文件时也会导致问题。这种事情发生的次数比人们愿意承认的要多。)但是你可以说:

class MyThing:
    processable = True
class MyOtherThing:
    processable = True
if getattr(obj, 'processable', False):
    process(obj)

添加继承就更好了:所有可处理对象的示例都可以继承

class Processable:
    processable = True

但你不需要说服每个人都继承你的基类,只需要设置一个属性。

操作符可以作为函数调用:

from operator import add
print reduce(add, [1,2,3,4,5,6])