java復制文件和java挪動文件的示例分享。本站提示廣大學習愛好者:(java復制文件和java挪動文件的示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java復制文件和java挪動文件的示例分享正文
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)
打印成果:
a = [1, 2, 3] b = [1, 2, 3] not_(a) : False truth(a) : True is_(a, b) : True is_not(a, b): False
可以經由過程成果曉得,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)
打印成果
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
這些函數等價於<、<=、==、>=和>的表達式語法。
算術操作符
處置數字的算術操作符也獲得支撐。
a, b, c, d = -1, 2, -3, 4 print 'a =', a print 'b =', b print 'c =', c print 'd =', d print '\nPositive/Negative:' print 'abs(a):', abs(a) print 'neg(a):', neg(a) print 'neg(b):', neg(b) print 'pos(a):', pos(a) print 'pos(b):', pos(b)
打印成果
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
abs前往值得相對值,neg前往(-obj), pos前往(+obj)。
a = -2 b = 5.0 print 'a =', a print 'b =', b print '\nArithmetic' print 'add(a, b) :', add(a, b) print 'div(a, b) :', div(a, b) print 'floordiv(a, b) :', floordiv(a, b) print 'mod(a, b) :', mod(a, b) print 'mul(a, b) :', mul(a, b) print 'pow(a, b) :', pow(a, b) print 'sub(a, b) :', sub(a, b) print 'truediv(a, b) :', truediv(a, b)
打印成果
a = -2 b = 5.0 Arithmetic add(a, b) : 3.0 div(a, b) : -0.4 floordiv(a, b) : -1.0 mod(a, b) : 3.0 # 檢查正數取模 mul(a, b) : -10.0 pow(a, b) : -32.0 sub(a, b) : -7.0 truediv(a, b) : -0.4
mod表現取模, mul 表現相乘,pow是次方, sub表現相減
a = 2 b = 6 print 'a =', a print 'b =', b print '\nBitwise:' print 'and_(a, b) :', and_(a, b) print 'invert(a) :', invert(a) print 'lshift(a, b) :', lshift(a, b) print 'or_(a, b) :', or_(a, b) print 'rshift(a, b) :', rshift(a, b) print 'xor(a, b) :', xor(a, b)
打印成果
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
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 = ['a', 'b'] print 'a =', a print 'b =', b print 'c =', c print 'd =', d print a = iadd(a, b) print 'a = iadd(a, b) =>', a print c = iconcat(c, d) print 'c = iconcat(c, d) =>', c
屬性和元素的獲得辦法
operator模塊最特殊的特征之一就是獲得辦法的概念,獲得辦法是運轉時結構的一些可回調對象,用來獲得對象的屬性或序列的內容,獲得辦法在處置迭代器或生成器序列的時刻特殊有效,它們引入的開支會年夜年夜下降lambda或Python函數的開支。
from operator import * class MyObj(object): def __init__(self, arg): super(MyObj, self).__init__() self.arg = arg def __repr__(self): return 'MyObj(%s)' % 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)
成果:
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)]
屬性獲得辦法相似於
lambda x, n='attrname':getattr(x,nz)
元素獲得辦法相似於
lambda x,y=5:x[y]
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)
成果以下:
dictionaries: [{'val': 0}, {'val': -1}, {'val': -2}, {'val': -3}] values: [0, -1, -2, -3] sorted: [{'val': -3}, {'val': -2}, {'val': -1}, {'val': 0}] tuples: [(0, 0), (1, -2), (2, -4), (3, -6)] values: [0, -2, -4, -6] sorted: [(3, -6), (2, -4), (1, -2), (0, 0)]
除序列以外,元素獲得辦法還實用於映照。
聯合操作符和定制類
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)
成果以下所示:
True MyObj(3)
類型檢討
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)
成果以下:
isMappingType(o): False isMappingType(t): True isNumberType(o): False isNumberType(t): True isSequenceType(o): False isSequenceType(t): True
然則這些測試其實不完美,由於托言沒有嚴厲界說。
獲得對象辦法
應用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('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