Overloading methods of subtype example.

CVS patchset: 6838
CVS date: 2003/05/12 22:28:28
This commit is contained in:
jbj 2003-05-12 22:28:28 +00:00
parent 03dc1c2738
commit e5b645cfe4
1 changed files with 28 additions and 0 deletions

28
python/sub.py Normal file
View File

@ -0,0 +1,28 @@
class Factory(object):
def __init__(self, false_self, method_name):
self.false_self = false_self
self.method_name = method_name
def __call__(self, val):
lself = long(self.false_self)
iself = int(self.false_self)
lm = long.__getattribute__(lself, self.method_name)
im = int.__getattribute__(iself, self.method_name)
la = lm(long(val))
ia = im(int(val))
print " Comparing", la, ia
assert la == ia
return la
class Bar(long):
def __getattribute__(self, name):
print "__getattribute__ ~%s~" % name
if name not in ('__add__', '__sub__', ):
return long.getattr(self, name)
return Factory(self, name)
a1 = Bar(1)
a2 = Bar(2)
print a1.__add__(a2)
print "Done"
print a1 + a2