博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
每周一个 Python 模块 | operator
阅读量:7170 次
发布时间:2019-06-29

本文共 5886 字,大约阅读时间需要 19 分钟。

专栏地址:

operator 模块是 Python 中内置的操作符函数接口,它定义了算术,比较和与标准对象 API 相对应的其他操作的内置函数。

operator 模块是用 C 实现的,所以执行速度比 Python 代码快。

逻辑运算

from operator import *a = -1b = 5print('a =', a)print('b =', b)print()print('not_(a)     :', not_(a))       # Falseprint('truth(a)    :', truth(a))	  # True		print('is_(a, b)   :', is_(a, b))	  # Falseprint('is_not(a, b):', is_not(a, b))  # True复制代码

not_()包括尾随下划线,因为not 是 Python 的关键字。 truth()作为判断表达式用在if语句中,或者将一个表达式转换成boolis_()is关键字的用法一样,is_not()用法相同,只不过返回相反的答案。

比较运算符

from operator import *a = 1b = 5.0print('a =', a)print('b =', b)for func in (lt, le, eq, ne, ge, gt):    print('{}(a, b): {}'.format(func.__name__, func(a, b)))    # a = 1# b = 5.0# lt(a, b): True# le(a, b): True# eq(a, b): False# ne(a, b): True# ge(a, b): False# gt(a, b): False    复制代码

功能是等同于使用表达式语法<<===>=,和>

算术运算符

from operator import *a = -1b = 5.0c = 2d = 6print('\nPositive/Negative:')print('abs(a):', abs(a))	# abs(a): 1print('neg(a):', neg(a))	# neg(a): 1print('neg(b):', neg(b))	# neg(b): -5.0print('pos(a):', pos(a))	# pos(a): -1print('pos(b):', pos(b))	# pos(b): 5.0print('\nArithmetic:')print('add(a, b)     :', add(a, b))			# add(a, b)     : 4.0print('floordiv(a, b):', floordiv(a, b))	# floordiv(a, b): -1.0print('floordiv(d, c):', floordiv(d, c))	# floordiv(d, c): 3print('mod(a, b)     :', mod(a, b))			# mod(a, b)     : 4.0print('mul(a, b)     :', mul(a, b))			# mul(a, b)     : -5.0print('pow(c, d)     :', pow(c, d))			# pow(c, d)     : 64print('sub(b, a)     :', sub(b, a))			# sub(b, a)     : 6.0print('truediv(a, b) :', truediv(a, b))		# truediv(a, b) : -0.2print('truediv(d, c) :', truediv(d, c))		# truediv(d, c) : 3.0print('\nBitwise:')print('and_(c, d)  :', and_(c, d))		# and_(c, d)  : 2		print('invert(c)   :', invert(c))		# invert(c)   : -3print('lshift(c, d):', lshift(c, d))	# lshift(c, d): 128print('or_(c, d)   :', or_(c, d))		# or_(c, d)   : 6print('rshift(d, c):', rshift(d, c))	# rshift(d, c): 1print('xor(c, d)   :', xor(c, d))		# xor(c, d)   : 4复制代码

序列运算符

使用序列的运算符可以分为四组:构建序列,搜索项目,访问内容以及从序列中删除项目。

from operator import *a = [1, 2, 3]b = ['a', 'b', 'c']print('\nConstructive:')print('  concat(a, b):', concat(a, b))		# concat(a, b): [1, 2, 3, 'a', 'b', 'c']print('\nSearching:')print('  contains(a, 1)  :', contains(a, 1))	# contains(a, 1)  : Trueprint('  contains(b, "d"):', contains(b, "d"))	# contains(b, "d"): Falseprint('  countOf(a, 1)   :', countOf(a, 1))		# countOf(a, 1)   : 1print('  countOf(b, "d") :', countOf(b, "d"))	# countOf(b, "d") : 0print('  indexOf(a, 5)   :', indexOf(a, 1))		# indexOf(a, 5)   : 0print('\nAccess Items:')print(getitem(b, 1))	# bprint(getitem(b, slice(1, 3))) # ['b', 'c']print(setitem(b, 1, "d")	# Noneprint(b)	# ['a', 'd', 'c']print(setitem(a, slice(1, 3), [4, 5]))	# Noneprint(a)	# [1, 4, 5]print('\nDestructive:')print(delitem(b, 1))	# Noneprint(b)	# ['a', 'c']print(delitem(a, slice(1, 3))	# Noneprint(a)	# [1]复制代码

其中一些操作(例如setitem()delitem())修改了序列并且不返回值。

原地操作符

除了标准运算符之外,许多类型的对象还支持通过特殊运算符进行“原地”修改 ,+=同样具有就地修改的功能:

from operator import *a = -1b = 5.0c = [1, 2, 3]d = ['a', 'b', 'c']a = iadd(a, b)print('a = iadd(a, b) =>', a)	# a = iadd(a, b) => 4.0c = iconcat(c, d)print('c = iconcat(c, d) =>', c)	# c = iconcat(c, d) => [1, 2, 3, 'a', 'b', 'c']复制代码

属性和元素的获取方法

operator 模块最特别的特性之一就是获取方法的概念,获取方法是运行时构造的一些可回调对象,用来获取对象的属性或序列的内容,获取方法在处理迭代器或生成器序列的时候特别有用,它们引入的开销会大大降低 lambda 或 Python 函数的开销。

from operator import *class MyObj:    """example class for attrgetter"""    def __init__(self, arg):        super().__init__()        self.arg = arg    def __repr__(self):        return 'MyObj({})'.format(self.arg)l = [MyObj(i) for i in range(5)]print(l)	# [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]# Extract the 'arg' value from each objectg = attrgetter('arg')vals = [g(i) for i in l]print('arg values:', vals)	# arg values: [0, 1, 2, 3, 4]# Sort using argl.reverse()print(l)	# [MyObj(4), MyObj(3), MyObj(2), MyObj(1), MyObj(0)]print(sorted(l, key=g))	# [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]复制代码

结合操作符和定制类

operator模块中的函数通过标准 Python 接口进行操作,因此它可以使用用户定义的类以及内置类型。

from operator import *class MyObj:    """Example for operator overloading"""    def __init__(self, val):        super(MyObj, self).__init__()        self.val = val    def __str__(self):        return 'MyObj({})'.format(self.val)    def __lt__(self, other):        """compare for less-than"""        print('Testing {} < {}'.format(self, other))        return self.val < other.val    def __add__(self, other):        """add values"""        print('Adding {} + {}'.format(self, other))        return MyObj(self.val + other.val)a = MyObj(1)b = MyObj(2)print('Comparison:')print(lt(a, b))		# Comparison:# Testing MyObj(1) < MyObj(2)# Trueprint('\nArithmetic:')print(add(a, b))	# Arithmetic:# Adding MyObj(1) + MyObj(2)# MyObj(3)复制代码

类型检查

operator 模块还包含一些函数用来测试映射、数字和序列类型的 API 兼容性。

from operator import *class NoType(object):    passclass MultiType(object):    def __len__(self):        return 0    def __getitem__(self, name):        return "mapping"    def __int__(self):        return 0o = NoType()t = MultiType()for func in [isMappingType, isNumberType, isSequenceType]:    print "%s(o):" % func.__name__, func(o)    print "%s(t):" % func.__name__, func(t)# isMappingType(o): False# isMappingType(t): True# isNumberType(o): False# isNumberType(t): True# isSequenceType(o): False# isSequenceType(t): True    复制代码

获取对象方法

使用 methodcaller 可以获取对象的方法。

from operator import methodcallerclass Student(object):    def __init__(self, name):        self.name = name    def getName(self):        return self.namestu = Student("Jim")func = methodcaller('getName')print func(stu)   # 输出Jim复制代码

还可以给方法传递参数:

f = methodcaller('name', 'foo', bar=1)f(b)    # return   b.name('foo', bar=1)复制代码

methodcaller方法等价于下面这个函数:

def methodcaller(name, *args,  **kwargs):      def caller(obj):            return getattr(obj, name)(*args, **kwargs)      return caller复制代码

相关文档:

转载地址:http://cnqwm.baihongyu.com/

你可能感兴趣的文章
Html5 布局方式
查看>>
day17--JQuery
查看>>
多维数组
查看>>
面试准备之java虚拟机内存结构
查看>>
基本的算法和数据结构的总结
查看>>
1119 Pre- and Post-order Traversals
查看>>
c++string类的简单介绍
查看>>
c#和数据库技术基础之程序设计集散地-数据库
查看>>
UITableView点击背景
查看>>
[Codeforces958E2]Guard Duty (medium)(区间DP)
查看>>
Javascript异步编程之二回调函数
查看>>
十大滤波算法
查看>>
MyBatis中井号与百分号的区别
查看>>
函数可变参数
查看>>
在appdelegate中 设置跟视图控制器 但是没办法全屏
查看>>
VS2017 启动调试报错无法启动程序 当前状态中非法
查看>>
JavaScript中的原型(prototype)与继承
查看>>
react-native-splash-screen android 启动页实现,以及遇到问题解决方案
查看>>
【转】线程正确的退出等待方式
查看>>
【Delphi】GIF 动画建立
查看>>