Revision 1 as of 2007-06-24 02:18:05

Clear message

{{{# This program demos overloading operator and some builtin function's hook。It can be used as a tutorial. # If you find any problem, please tell me. # author: Simeon Chaos simeon.chaos@gmail.com # version 0.1.0 2007-6-24 # license: In public domain. You can freely use, modify, redistribute this file in any ways. Keep the text about the authon is appreciated. # Warranty: THERE IS NO ANY WARRANTY FOR THE PROGRAM. The software is provided "as-is," without any express or implied warranty. In no event shall the author be held liable for any damages arising from the use of the software. # Tested at Windows, python 2.4.4

#from future import division #when imported,truediv is called, output is as below: # op/1: truediv 1/op: rtruediv op /= 1: itruediv #else div is called, output is as below: # op/1: div 1/op: rdiv op /= 1: idiv

# Chinese # 本程序演示重载操作符和某些内置函数的钩子,用于学习和演示。 # 如果有任何问题,请告诉我。 # 作者: 曹星明 simeon.chaos@gmail.com # 版本 0.1.0 2007-6-24 # 使用证书:公用许可。允许以任何方式自由使用,修改,分发本文件。欢迎保留这些关于作者的文字。 # 免责声明:作者对本文件的使用不作任何担保。 # 在Windows, python 2.4.4上运行通过。

#from future import division #如果导入,将调用truediv,因此将有如下输出 # op/1: truediv 1/op: rtruediv op /= 1: itruediv #否则,将调用div,因此将有如下输出 # op/1: div 1/op: rdiv op /= 1: idiv

#result(run on Windows, python 2.4.4, boa constructor 0.4.4) op.doc: Operation doc Demo the relation between some operators / builtin function and their hook method. 演示各种操作符以及内建函数与钩子函数之间的对应关系 op+1: add 1+op: radd op += 1: iadd op-1: sub 1-op: rsub op -= 1: isub op*1: mul 1*op: rmul op *= 1: imul op/1: div 1/op: rdiv op /= 1: idiv op%1: xmod 1%op: rmod op %= 1: imod op//1: floordiv 1//op: rfloordiv op //= 1: ifloordiv op1: __xor__ 1op: rxor op ^= 1: ixor ~op: invert -op: neg +op: pos abs(op): abs op==1: eq 1==op: eq op!=1: ne 1!=op: ne op<1: lt 1<op: gt op<=1: le 1<=op: ge op>=1: ge 1>=op: le op>1: gt 1>op: lt cmp(op,1): eq lt gt cmp cmp(1,op): eq gt lt cmp op>=1: ge 1>=op: le divmod(op,1): divmod divmod(1,op): rdivmod pow(op,1): pow pow(1,op): rpow int(op): int long(op): long float(op): float oct(op): oct hex(op): hex op or 1: nonzero op(): call op[1]=1 setitem op[1] getitem op[:]=[] setslice op[:] getslice del op[:] delslice 1 in op contains [x for x in op] iter op.a: getattr op.a = 1: setattr op.a: del op.a: delattr

len(op) len str(op) str repr(op) repr

coerce(op,1): coerce number coercion failed op+1: coerce

class Operation:

Demo the relation between some operators / builtin function and their hook method. 演示各种操作符以及内建函数与钩子函数之间的对应关系

op = Operation() print 'op.doc:', op.doc

print 'op+1:',; op+1; print ' 1+op:',; 1+op; print ' op += 1:',; op += 1; print print 'op-1:',; op-1; print ' 1-op:',; 1-op; print ' op -= 1:',; op -= 1; print print 'op*1:',; op*1; print ' 1*op:',; 1*op; print ' op *= 1:',; op *= 1; print print 'op/1:',; op/1; print ' 1/op:',; 1/op; print ' op /= 1:',; op /= 1; print print 'op%1:',; op%1; print ' 1%op:',; 1%op; print ' op %= 1:',; op %= 1; print print 'op//1:',; op//op; print ' 1//op:',; 1//op; print ' op //= 1:',; op //= 1; print print 'op1:',; op1; print ' 1op:',; 1op; print ' op = 1:',; op = 1; print

print '~op:',; ~op print '-op:',; -op print '+op:',; +op

print 'abs(op):',; abs(op);print print 'op==1:',; op==1; print ' 1==op:',; 1==op; print print 'op!=1:',; op!=1; print ' 1!=op:',; 1!=op; print print 'op<1:',; op<1; print ' 1<op:',; 1<op; print print 'op<=1:',; op<=1; print ' 1<=op:',; 1<=op; print print 'op>=1:',; op>=1; print ' 1>=op:',; 1>=op; print print 'op>1:',; op>1; print ' 1>op:',; 1>op; print print 'cmp(op,1):',; cmp(op,1); print ' cmp(1,op):',; cmp(1,op);print print 'op>=1:',; op>=1; print ' 1>=op:',; 1>=op;print print 'divmod(op,1):',;divmod(op,1);print ' divmod(1,op):',; divmod(1,op); print print 'pow(op,1):',;pow(op,1);print ' pow(1,op):',; pow(1,op); print

print 'int(op):',; int(op) print 'long(op):',; long(op) print 'float(op):',; float(op) print 'oct(op):',; oct(op) print 'hex(op):',; hex(op)

print 'op or 1:',; op or 1 print 'op():',; op(); print

print 'op[1]=1',; op[1]=1 print 'op[1]',; op[1] print 'op[:]=[]',; op[:]=[] print 'op[:]',; op[:] print 'del op[:]',; del op[:] print '1 in op',; 1 in op print '[x for x in op]',;[x for x in op] print 'op.a:',; op.a; print print 'op.a = 1:',; op.a = 1 print 'op.a:',; op.a; print print 'del op.a:',; del op.a; print print 'len(op)',; len(op); print 'str(op)',; str(op) print 'repr(op)', repr(op)

class Operation:

so coerce is put here alone. You can try it yourself. 补充:因为coerce放在上面的类定义中将干扰其它方法的执行,因此单独列出。读者可以自己试一下。

op = Operation() try: print 'coerce(op,1):',; coerce(op,1); except TypeError, e: print e try: print 'op+1:',; op+1; except TypeError, e: print e }}}


CategoryLanguage

Unable to edit the page? See the FrontPage for instructions.