Possible to fake object type?

Chris Liechti cliechti at gmx.net
Sat Mar 2 11:59:52 EST 2002


Michael 'Mickey' Lauer <mickey at tm.informatik.uni-frankfurt.de> wrote 
in news:3c80fb6d at nntp.server.uni-frankfurt.de:

> Chris Liechti <cliechti at gmx.net> wrote:
>> Michael 'Mickey' Lauer <mickey at tm.informatik.uni-frankfurt.de> 
wrote 
>> in news:3c80e76b at nntp.server.uni-frankfurt.de: 
>>> So: Is it possible to fake the type of a class or
>>> is it possible to magically override the method call "bouncing" 
to
>>> the base class ? 
>> 
>> you can allways call the methods of on e of the supperclasses by
>> using: 
>> 
>> Baseclass.method(self, args, ...)
>> 
>> it's the same you do in __init__ to initilaize base classes:
>> 
>> class Dockable(gtk.HandleBox):
>>        def __init__(self): gtk.HandleBox.__init__(self)
>> 
>>        def dosomething(self, arg):
>>                print "calling dosomething...", result =
>>                gtk.HandleBox.dosomething(self, arg) print "result 
=
>>                %r" % result return result 
> 
> Yeah, sure, but I'd have to do this for every single
> function - which is not quite the elegant solution
> I aimed to come up with by using the getattr magic.

why not something like that?:

def __getattr__(self, name):
    	try:
    	    	return self.__dict__[name]
    	except NameError:
    	    	return getattr(gtk.HandleBox, name)

something similar can be done for setattr too of course.

you could also google for the Proxy Design Pattern which can be 
implemented in such a way.

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list