From e5b645cfe4edc921fd5c7694beff84b27df988a4 Mon Sep 17 00:00:00 2001 From: jbj Date: Mon, 12 May 2003 22:28:28 +0000 Subject: [PATCH] Overloading methods of subtype example. CVS patchset: 6838 CVS date: 2003/05/12 22:28:28 --- python/sub.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 python/sub.py diff --git a/python/sub.py b/python/sub.py new file mode 100644 index 0000000..6ad57a7 --- /dev/null +++ b/python/sub.py @@ -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 -- 2.7.4