==operator 模块==``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类的函数的时候, ``operator`` 模块中的函数可以替换一些 ``lambda`` 函式. 而且这些函数在一些喜欢写晦涩代码的程序员中很流行. [Example 1-62 #eg-1-62] 展示了 ``operator`` 模块的一般用法.====Example 1-62. 使用 operator 模块====[eg-1-62]```File: operator-example-1.pyimport operatorsequence = 1, 2, 4print "add", "=>", reduce(operator.add, sequence)print "sub", "=>", reduce(operator.sub, sequence)print "mul", "=>", reduce(operator.mul, sequence)print "concat", "=>", operator.concat("spam", "egg")print "repeat", "=>", operator.repeat("spam", 5)print "getitem", "=>", operator.getitem(sequence, 2)print "indexOf", "=>", operator.indexOf(sequence, 2)print "sequenceIncludes", "=>", operator.sequenceIncludes(sequence, 3)*B*add => 7sub => -5mul => 8concat => spameggrepeat => spamspamspamspamspamgetitem => 4indexOf => 1sequenceIncludes => 0*b*```[Example 1-63 #eg-1-63] 展示了一些可以用于检查对象类型的 ``operator`` 函数.====Example 1-63. 使用 operator 模块检查类型====[eg-1-63]```File: operator-example-2.pyimport operatorimport UserListdef dump(data): print type(data), "=>", if operator.isCallable(data): print "CALLABLE", if operator.isMappingType(data): print "MAPPING", if operator.isNumberType(data): print "NUMBER", if operator.isSequenceType(data): print "SEQUENCE", print dump(0)dump("string")dump("string"[0])dump([1, 2, 3])dump((1, 2, 3))dump({"a": 1})dump(len) # function 函数dump(UserList) # module 模块dump(UserList.UserList) # class 类dump(UserList.UserList()) # instance 实例*B*=> NUMBER => SEQUENCE => SEQUENCE => SEQUENCE => SEQUENCE => MAPPING => CALLABLE => => CALLABLE => MAPPING NUMBER SEQUENCE*b*```这里需要注意 ``operator`` 模块使用非常规的方法处理对象实例. 所以使用 ``isNumberType`` , ``isMappingType`` , 以及 ``isSequenceType`` 函数的时候要小心, 这很容易降低代码的扩展性.同样需要注意的是一个字符串序列成员 (单个字符) 也是序列. 所以当在递归函数使用 isSequenceType 来截断对象树的时候, 别把普通字符串作为参数(或者是任何包含字符串的序列对象).