python标准库介绍——8 operator 模块详解
==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 来截断对象树的时候, 别把普通字符串作为参数(或者是任何包含字符串的序列对象).
python标准库介绍——8 operator 模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- python标准库介绍——22 UserList 模块详解
==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...
- python标准库介绍——21 UserDict 模块详解
==UserDict 模块== ``UserDict`` 模块包含了一个可继承的字典类 (事实上是对内建字典类型的 Python 封装). [Example 2-15 #eg-2-15] 展示了一个增 ...
- python标准库介绍——20 cStringIO 模块详解
==cStringIO 模块== ``cStringIO`` 是一个可选的模块, 是 ``StringIO`` 的更快速实现. 它的工作方式和 ``StringIO`` 基本相同, 但是它不可以被继承 ...
随机推荐
- Dynamic Programming for TSP
See how Dynamic programming working for TSP: Check this link: http://www.youtube.com/watch?v=IUzE1Mb ...
- C++学习笔记12-模板1
1. 函数模板 函数模板是一个独立于类型的函数,可作为一种方式.产生函数的特定类型版本号. // implement strcmp-like generic compare function // ...
- android 动画具体解释(二)
以下就開始学习属性动画的基本使用方法,我们来看属性动画的继承关系,例如以下如所看到的: 显然关注的焦点应该是ValueAnimator,ObjectAnimator这两个类啦,ObjectAnimat ...
- STL - 容器 - Forward List
forward list是一个行为受限的list, 不能走回头路. 它只提供前向迭代器, 而不提供双向迭代器. eg: rbegin(), rend(), crbegin(), crend()这些都不 ...
- 嵌入式web服务器-thttpd
交叉编译thttpd http://lakie.blog.163.com/blog/static/45185220201162910432330/ thttpd安装与调试 http://blog.cs ...
- 天气预报的Ajax效果
最近在网站上看了很多显示实时天气预报的,挺实用而且用户体验也不错.对用户的帮助也比较大,用户可以通过你的网站了解到实时的天气信息.感觉比较有意思,于是自己钻研了一下其中的实现方法.于是决定把代码分享给 ...
- Tomcat日志格式自定义
设置日志显示信息格式,默认情况下,Tomcat的访问日志是不记录的.需要在serve.xml中修改配置,去掉注释. <!-- <Valve className=&q ...
- Hibernate日期映射类型
映 射 类 型 Java类型 标准SQL类型 描 述 date java.util.Date或者java.sql.Date DATE 代表日期,形式为: YYYY-MM-DD time java ...
- java线程具体解释
线程与进程的差别 (1)程序是一段静态的代码,进程是程序的一次动态执行过程.它是操作系统资源调度的基本单位.线程是比进程更小的执行单位.一个进程在其执行过程中,能够产生多个线程.所以又称线程为&quo ...
- DB2的认证和授权
DB2 的安全性由两方面组成:认证和授权 1.认证 认证就是系统验证用户身份的过程.说的简单点,就是验证用户名和密码,因为DB2用户同时也是操作系统用户,所以,首先必须得到操作系统的认可.在默认情况下 ...