Possible to fake object type?

Michael 'Mickey' Lauer mickey at tm.informatik.uni-frankfurt.de
Sat Mar 2 08:53:31 EST 2002


Hi,

suppose I have the following code:

class Dockable:
    "Represents a dockable window"
    
    def __init__( self, childclass, handlepos = gtk.POS_LEFT ):
        self.__dict__["_handlebox"] = gtk.HandleBox()
        self.__dict__["_child"] = childclass()
        self._handlebox.add( self._child )

    def __getattr__( self, attr ):
        print "requesting attribut: '%s'" % attr
        return self.__dict__.get( attr, None ) \
            or getattr( self._child, attr )



Dockable is composition of two classes trying to
mimick the interface of the childclass and thus hiding
that the childclass is embedded in a HandleBox.

So far so good. The Problem: PyGTK (which I'm using)
has typechecking. I cannot use my composition class
instead of a - say gtk.HandleBox(), because I get, for instance:

TypeError: child should be a GtkWidget

It's no option to make Dockable a GtkWidget, for instance,
by deriving it from gtk.HandleBox, because GtkWidget itself
contains methods which I call on a Dockable but which should
be reflected to the _child rather than be handled by the
- then - baseclass GtkWidget.

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 ?

Yours,

:M:
--
|----------------------------------------------------------------------------|
| Dipl.-Inf. Michael 'Mickey' Lauer    mickey at tm.informatik.uni-frankfurt.de |
|   Raum 10b - ++49 69 798 28358        Fachbereich Informatik und Biologie  |
|----------------------------------------------------------------------------|



More information about the Python-list mailing list