missing methods or __getattr__ problem

Mark McEahern marklists at mceahern.com
Wed Oct 2 20:39:02 CEST 2002


Try this:

#!/usr/bin/env python

import sys

def method_invoked(inst, args, kwargs):
    steps_back = 1
    func = sys._getframe(steps_back).f_code.co_name
    print "%s.%s(%s, %s)" % (inst.__class__.__name__, func, args, kwargs)

class A:

    def a(self, *args, **kwargs):
        method_invoked(self, args, kwargs)

class B:

    def b(self, *args, **kwargs):
        method_invoked(self, args, kwargs)

class C:

    def c(self, *args, **kwargs):
        method_invoked(self, args, kwargs)

a = A()
a.a(1, 2, 3)

class Proxy:

    def __init__(self, *classes):
        self.classes = classes

    def __getattr__(self, name):
        for c in self.classes:
            for c_name in dir(c):
                if c_name == name:
                    return getattr(c(), name)
        raise AttributeError(name)

p = Proxy(A, B, C)
p.a(1, 2, 3)

p.b(1, 2, 4)

p.d(1, 2, 4)




More information about the Python-list mailing list