python标准库介绍——8 operator 模块详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | = = 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.py import operator sequence = 1 , 2 , 4 print "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 = > 7 sub = > - 5 mul = > 8 concat = > spamegg repeat = > spamspamspamspamspam getitem = > 4 indexOf = > 1 sequenceIncludes = > 0 * b * ``` [Example 1 - 63 #eg-1-63] 展示了一些可以用于检查对象类型的 ``operator`` 函数. = = = = Example 1 - 63. 使用 operator 模块检查类型 = = = = [eg - 1 - 63 ] ``` File : operator - example - 2.py import operator import UserList def 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 * < type 'int' > = > NUMBER < type 'string' > = > SEQUENCE < type 'string' > = > SEQUENCE < type 'list' > = > SEQUENCE < type 'tuple' > = > SEQUENCE < type 'dictionary' > = > MAPPING < type 'builtin_function_or_method' > = > CALLABLE < type 'module' > = > < type 'class' > = > CALLABLE < type 'instance' > = > MAPPING NUMBER SEQUENCE * b * ``` 这里需要注意 ``operator`` 模块使用非常规的方法处理对象实例. 所以使用 ``isNumberType`` , ``isMappingType`` , 以及 ``isSequenceType`` 函数的时候要小心, 这很容易降低代码的扩展性. 同样需要注意的是一个字符串序列成员 (单个字符) 也是序列. 所以当在递归函数使用 isSequenceType 来截断对象树的时候, 别把普通字符串作为参数(或者是任何包含字符串的序列对象). |
如果觉得对您有帮助,麻烦您点一下推荐,谢谢!
好记忆不如烂笔头
好记忆不如烂笔头
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步