Differences between revisions 1 and 2
Revision 1 as of 2007-06-24 02:18:05
Size: 10591
Editor: hn
Comment:
Revision 2 as of 2007-06-24 02:26:20
Size: 10589
Editor: hn
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
# license: In public domain. You can freely use, modify, redistribute this file in any ways. Keep the text about the authon is appreciated. # license: In public domain. You can freely use, modify, redistribute this file in any ways. Keep the text about the author is appreciated.
Line 242: Line 242:
  '''complement:If __coerce__ is put above, it is disturbed to run the other methoded,   '''complement:If __coerce__ is put above, it is disturbed to run the other method,

{{{# 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 author 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:

  • Operation doc

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

  • def add(self, other): print 'add', def radd(self, other): print 'radd', def iadd(self, other): print 'iadd',; return self

    def sub(self, other): print 'sub', def rsub(self, other): print 'rsub', def isub(self, other): print 'isub',; return self

    def mul(self, other): print 'mul', def rmul(self, other): print 'rmul', def imul(self, other): print 'imul',; return self

    def div(self, other): print 'div', def rdiv(self, other): print 'rdiv', def idiv(self, other): print 'idiv',; return self

    def xor(self, other): print 'xor', def rxor(self, other): print 'rxor', def ixor(self, other): print 'ixor',; return self

    def mod(self, other): print 'xmod', def rmod(self, other): print 'rmod', def imod(self, other): print 'imod',; return self

    def floordiv(self, other): print 'floordiv', def rfloordiv(self, other): print 'rfloordiv', def ifloordiv(self, other): print 'ifloordiv',; return self

    def truediv(self, other): print 'truediv', def rtruediv(self, other): print 'rtruediv', def itruediv(self, other): print 'itruediv',; return self

    def or(self, other): print 'or', def ror(self, other): print 'ror', def ior(self, other): print 'ior',; return self

    def and(self, other): print 'and', def rand(self, other): print 'rand', def iand(self, other): print 'iand',; return self

    def lshift(self, other): print 'lshift', def rlshift(self, other): print 'rlshift', def ilshift(self, other): print 'ilshift',; return self

    def rshift(self, other): print 'rshift', def rrlshift(self, other): print 'rrshift', def irlshift(self, other): print 'irshift',; return self

    def invert(self): print 'invert'; return 0 def neg(self): print 'neg'; return -1 def pos(self): print 'pos'; return +1

    def divmod(self, other): print 'divmod', def rdivmod(self, other): print 'rdivmod',

    def pow(self, other): print 'pow', def rpow(self, other): print 'rpow',

    def abs(self): print 'abs', def nonzero(self): print 'nonzero'; return True

    def call(self): print 'call',;

    def eq(self, other): print 'eq',; return 0 def ne(self, other): print 'ne',; return 0 def lt(self, other): print 'lt',; return 0 def le(self, other): print 'le',; return 0 def ge(self, other): print 'ge',; return 0 def gt(self, other): print 'gt',; return 0 def cmp(self, other): print 'cmp',; return 0

    def coerce(self, other): return None

    def getitem(self, i): print 'getitem'; return None def setitem(self, i, value): print 'setitem'; return self

    def getslice( self, i, j): print 'getslice'; return self

    • #Deprecated since release 2.0. Support slice objects as parameters to the getitem() method.

    def setslice( self, i, j, sequence): print 'setslice'; return self

    • #This method is deprecated.

    def delslice( self, i, j): print 'delslice' def contains( self, item): print 'contains' def iter( self): print 'iter'; return iter([])

    def getattribute(self, attr): print 'getattribute'; return None #def setattribute(self, attr, value): print 'setattribute'; self.dict[attr] = value; return None #setattribute只对new style class起作用 def getattr(self, attr): print 'getattr',; return None def setattr(self, attr, value): print 'setattr'; self.dict[attr] = value; return None def delattr(self, attr): print 'delattr'; return None

    def float(self): print 'float'; return 0.0 def int(self): print 'int'; return 0 def long(self): print 'long'; return 0 def oct(self): print 'oct'; return '12' def hex(self): print 'hex'; return '0xf5' def len(self): print 'len'; return 0 def repr(self): print 'repr'; return def str(self): print 'str'; return

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:

  • complement:If coerce is put above, it is disturbed to run the other method,

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

  • def add(self, other): pass def coerce(self, other): print 'coerce',; return None

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

OperatorHook (last edited 2013-10-06 13:04:43 by PaulBoddie)

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