[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] A question about callback functions



mike@xxxxxxxxx wrote:
Using C, I would have probably implemented a system using function
pointers to create callbacks but Python doesn't seem to have such
functionality. What techniques would anyone else suggest to implement such
a feature in 'pure' Python?

function pointer are something that is called in C++ virtual method... (the vtable in C++ is just a table for func pointers).

Now if you try to translate the concept of virtual method in Python you get?

instance methods :)

So basically in Python you would let people create their own class that implements a specific set of callbacks.

class MyEnemy:
	def player_moved(self,a,b):
		self.shoot_to(a+1,b+1)
	def player_shoots(self,a,b):
		if self.pos == (a,b):
			self.move_a_bit()


or something like this :)

And then when reading the configuration you would just need to instantiate the class from its name (using apply for example).

Regards,

Guillaume