Python中operator模块的操作符使用

高洛峰
Libérer: 2017-03-01 14:13:12
original
1164 Les gens l'ont consulté

operator模块是python中内置的操作符函数接口,它定义了一些算术和比较内置操作的函数。operator模块是用c实现的,所以执行速度比python代码快。

逻辑操作

from operator import *

a = [1, 2, 3]
b = a
print 'a =', a
print 'b =', b
print 

print 'not_(a)   :', not_(a)
print 'truth(a)   :', truth(a)
print 'is_(a, b)  :', is_(a, b)
print 'is_not(a, b) :', is_not(a, b)
Copier après la connexion

打印结果:

a = [1, 2, 3]
b = [1, 2, 3]
not_(a)   : False
truth(a)  : True
is_(a, b)  : True
is_not(a, b): False
Copier après la connexion

可以通过结果知道,operator的一些操作函数与原本的运算是相同的。

比较操作符
operator提供丰富的比较操作。

a = 3
b = 5
print 'a =', a
print 'b =', b
print 

for func in (lt, le, eq, ne, ge, gt):
  print '{0}(a, b):'.format(func.__name__), func(a, b)
Copier après la connexion

打印结果

a = 3
b = 5

lt(a, b): True
le(a, b): True
eq(a, b): False
ne(a, b): True
ge(a, b): False
gt(a, b): False
Copier après la connexion

这些函数等价于<、<=、==、>=和>的表达式语法。

算术操作符
处理数字的算术操作符也得到支持。

a, b, c, d = -1, 2, -3, 4

print &#39;a =&#39;, a
print &#39;b =&#39;, b
print &#39;c =&#39;, c
print &#39;d =&#39;, d
 
print &#39;\nPositive/Negative:&#39;
print &#39;abs(a):&#39;, abs(a)
print &#39;neg(a):&#39;, neg(a)
print &#39;neg(b):&#39;, neg(b)
print &#39;pos(a):&#39;, pos(a)
print &#39;pos(b):&#39;, pos(b)
Copier après la connexion

打印结果

a = -1
b = 2
c = -3
d = 4

Positive/Negative:
abs(a): 1
neg(a): 1
neg(b): -2
pos(a): -1
pos(b): 2
Copier après la connexion

abs返回值得绝对值,neg返回(-obj), pos返回(+obj)。

a = -2
b = 5.0

print &#39;a =&#39;, a
print &#39;b =&#39;, b
 
print &#39;\nArithmetic&#39;
print &#39;add(a, b)    :&#39;, add(a, b)
print &#39;p(a, b)    :&#39;, p(a, b)
print &#39;floorp(a, b)  :&#39;, floorp(a, b)
print &#39;mod(a, b)    :&#39;, mod(a, b)
print &#39;mul(a, b)    :&#39;, mul(a, b)
print &#39;pow(a, b)    :&#39;, pow(a, b)
print &#39;sub(a, b)    :&#39;, sub(a, b)
print &#39;truep(a, b)  :&#39;, truep(a, b)
Copier après la connexion

打印结果

a = -2
b = 5.0

Arithmetic
add(a, b)    : 3.0
p(a, b)    : -0.4
floorp(a, b)  : -1.0
mod(a, b)    : 3.0 # 查看负数取模
mul(a, b)    : -10.0
pow(a, b)    : -32.0
sub(a, b)    : -7.0
truep(a, b)  : -0.4
Copier après la connexion

mod表示取模, mul 表示相乘,pow是次方, sub表示相减

a = 2
b = 6

print &#39;a =&#39;, a
print &#39;b =&#39;, b

print &#39;\nBitwise:&#39;
print &#39;and_(a, b)  :&#39;, and_(a, b)
print &#39;invert(a)  :&#39;, invert(a)
print &#39;lshift(a, b) :&#39;, lshift(a, b)
print &#39;or_(a, b)  :&#39;, or_(a, b)
print &#39;rshift(a, b) :&#39;, rshift(a, b)
print &#39;xor(a, b)  :&#39;, xor(a, b)
Copier après la connexion

打印结果

a = 2
b = 6

Bitwise:
and_(a, b)  : 2
invert(a)  : -3
lshift(a, b) : 128
or_(a, b)  : 6
rshift(a, b) : 0
xor(a, b)  : 4
Copier après la connexion

and 表示按位与, invert 表示取反操作, lshift表示左位移, or表示按位或, rshift表示右位移,xor表示按位异或。


原地操作符
即in-place操作, x += y 等同于 x = iadd(x, y), 如果复制给其他变量比如z = iadd(x, y)等同与z = x; z += y。

a = 3
b = 4
c = [1, 2]
d = [&#39;a&#39;, &#39;b&#39;]

print &#39;a =&#39;, a
print &#39;b =&#39;, b
print &#39;c =&#39;, c
print &#39;d =&#39;, d
print

a = iadd(a, b)
print &#39;a = iadd(a, b) =>&#39;, a
print

c = iconcat(c, d)
print &#39;c = iconcat(c, d) =>&#39;, c
Copier après la connexion

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

from operator import *
class MyObj(object):
  def __init__(self, arg):
    super(MyObj, self).__init__()
    self.arg = arg
  def __repr__(self):
    return &#39;MyObj(%s)&#39; % self.arg

objs = [MyObj(i) for i in xrange(5)]
print "Object:", objs

g = attrgetter("arg")
vals = [g(i) for i in objs]
print "arg values:", vals

objs.reverse()
print "reversed:", objs
print "sorted:", sorted(objs, key=g)
Copier après la connexion

结果:

Object: [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]
arg values: [0, 1, 2, 3, 4]
reversed: [MyObj(4), MyObj(3), MyObj(2), MyObj(1), MyObj(0)]
sorted: [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]
Copier après la connexion

属性获取方法类似于

lambda x, n=&#39;attrname&#39;:getattr(x,nz)
Copier après la connexion

元素获取方法类似于

lambda x,y=5:x[y]
Copier après la connexion

from operator import *

l = [dict(val=-1*i) for i in xrange(4)]
print "dictionaries:", l
g = itemgetter("val")
vals = [g(i) for i in l]
print "values: ", vals
print "sorted:", sorted(l, key=g)

l = [(i,i*-2) for i in xrange(4)]
print "tuples: ", l
g = itemgetter(1)
vals = [g(i) for i in l]
print "values:", vals
print "sorted:", sorted(l, key=g)
Copier après la connexion

结果如下:

dictionaries: [{&#39;val&#39;: 0}, {&#39;val&#39;: -1}, {&#39;val&#39;: -2}, {&#39;val&#39;: -3}]
values: [0, -1, -2, -3]
sorted: [{&#39;val&#39;: -3}, {&#39;val&#39;: -2}, {&#39;val&#39;: -1}, {&#39;val&#39;: 0}]
tuples: [(0, 0), (1, -2), (2, -4), (3, -6)]
values: [0, -2, -4, -6]
sorted: [(3, -6), (2, -4), (1, -2), (0, 0)]
Copier après la connexion

除了序列之外,元素获取方法还适用于映射。

结合操作符和定制类
operator模块中的函数通过相应操作的标准Python接口完成工作,所以它们不仅适用于内置类型,还适用于用户自定义类型。

from operator import *

class MyObj(object):
  def __init__(self, val):
    super(MyObj, self).__init__()
    self.val = val
    return 

  def __str__(self):
    return "MyObj(%s)" % self.val

  def __lt__(self, other):
    return self.val < other.val

  def __add__(self, other):
    return MyObj(self.val + other.val)

a = MyObj(1)
b = MyObj(2)

print lt(a, b)
print add(a,b)
Copier après la connexion

结果如下所示:

True
MyObj(3)
Copier après la connexion

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

from operator import *

class NoType(object):
  pass

class MultiType(object):
  def __len__(self):
    return 0

  def __getitem__(self, name):
    return "mapping"

  def __int__(self):
    return 0

o = NoType()
t = MultiType()

for func in [isMappingType, isNumberType, isSequenceType]:
  print "%s(o):" % func.__name__, func(o)
  print "%s(t):" % func.__name__, func(t)
Copier après la connexion

结果如下:

isMappingType(o): False
isMappingType(t): True
isNumberType(o): False
isNumberType(t): True
isSequenceType(o): False
isSequenceType(t): True
Copier après la connexion

但是这些测试并不完善,因为借口没有严格定义。

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

from operator import methodcaller

class Student(object):
  def __init__(self, name):
    self.name = name

  def getName(self):
    return self.name

stu = Student("Jim")
func = methodcaller(&#39;getName&#39;)
print func(stu)  # 输出Jim
Copier après la connexion

还可以给方法传递参数:

f=methodcaller(&#39;name&#39;, &#39;foo&#39;, bar=1)
f(b)  # return  b.name(&#39;foo&#39;, bar=1)
methodcaller方法等价于下面这个函数:

def methodcaller(name, *args, **kwargs):
   def caller(obj):
      return getattr(obj, name)(*args, **kwargs)
   return caller
Copier après la connexion


更多Python中operator模块的操作符使用相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!