inheritance problem

gabor gabor at
Tue Oct 1 18:08:53 CEST 2002


i have the following problem:

i'm using GLUT to create an opengl application but this is not important

let's say i'm using an api which uses callback functions for things like
onkeypress etc...

now i want to wrap it in an object-oriented manner:

the basic problem is that how to route the event-handlers to the correct

i solved it like this:

i created a base class ( let's call it BaseClass):
	its constructor does some basic initialization, then creates a new
window, and receives a windowID... this is an unique number...
	so he saves the windowID + self in a dictionary

	then he registers the callback functions..

	the callback functions work like this:

def KeyPressCallback(key):
	currentWindowID = glutGetWindow()
	instance = objects[currentWindowID]

	and so on

	and of course the base class had some methods which were empty 	ones,
to be overriden by the inherited class's methods
	so for example:
def displayData(self):
	#this method is called from opengl..

then i created an inherited class (let's call it InheritedClass):
	and override displayData

	but there's the problem:
	in that windowIDdictionary, the type of the self's is BaseClass,
	so it won't call InheritedClass's methods :-((((

any ideas how to do this correctly?
as i know there is nothing like casting in python :-((

i have some solutions like the inerited class's constructor will have to
manually call a method in the baseclass.... but i wanted to make it a
way, so the inherited class doesn't have to deal with the baseclass,
except for the constructor...


That's life for you, said McDunn.  Someone always waiting for someone 
who never comes home.  Always someone loving something more than that 
thing loves them.  And after awhile you want to destroy whatever 
that thing is, so it can't hurt you no more.
                -- R. Bradbury, "The Fog Horn"

More information about the Python-list mailing list