Transfer undefined class methods to attribute's method.

Maarten van Reeuwijk maarten at remove_this_ws.tn.tudelft.nl
Mon Feb 9 09:17:21 EST 2004


Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class should
be delegated (I can't use inheritance, see other post). It should be
possible to achieve this using Python's great introspection possibilities,
but I can't find out how. Any help would be really appreciated!

TIA, Maarten

Example (class A and Proxy): 

class A:
    def __init__(self):
        pass

    def methodA(self):
        pass

    def methodB(self):
        pass

    def methodC(self):
        pass

class Proxy:
    def __init__(self):
        self.a = A()

        # maybe scan the methods in A and not in this class?????
        # setup a hook for undefined methods?

    def methodA(self):
        # what I DON'T want:
        return self.a.methodA()
        # and this for every method...


    # maybe something like this?
    def process_unknownmethod(self, method, args):
        return self.a.method(args)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
  File "group.py", line 36, in ?
    P.methodC()



-- 
===================================================================
Maarten van Reeuwijk                    Thermal and Fluids Sciences
Phd student                             dept. of Multiscale Physics
www.ws.tn.tudelft.nl                 Delft University of Technology



More information about the Python-list mailing list