operator
2019年6月4日小于 1 分钟约 215 字
关于operator标准库:
>>> help(operator)
Help on module operator:
NAME
operator - Operator interface.
DESCRIPTION
This module exports a set of functions implemented in C corresponding
to the intrinsic operators of Python. For example, operator.add(x, y)
is equivalent to the expression x+y. The function names are those
used for special methods; variants without leading and trailing
'__' are also provided for convenience.
CLASSES
builtins.object
attrgetter
itemgetter
methodcaller
通过operator模块的itemgetter()
函数,可以非常容易地根据关键字排序字典列表:
操作 | 说明 |
---|---|
![]() | ![]() |
![]() | itemgetter() 函数也支持多个keys |
![]() | itemgetter() 有时候也可以用lambda表达式替代;这种方案不错,但是 itemgetter() 方式会稍快一点。 |
![]() | 其实,itemgetter() 操作也适用于min() 和max() 函数 |
operator.methodcaller()
函数可通过字符串形式的方法名称调用某个对象的对应方法。见“类”页面笔记。