Skip to content

Instantly share code, notes, and snippets.

@brycethomas
Created June 16, 2012 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycethomas/2941813 to your computer and use it in GitHub Desktop.
Save brycethomas/2941813 to your computer and use it in GitHub Desktop.
Function pointers up to delegates
# some button class somewhere that you didn't write.
class Button():
def __init__(self,functionToExecuteOnClick):
self.clickFunction = functionToExecuteOnClick
# does a bunch of other cool stuff to display button - you were too slack to write this code, that's why you're using their button in the first place.
# pretend this is private. other classes shouldn't be calling this method
def buttonClicked():
self.clickFunction(self.name)
# now off in your own code
def myClickFunction():
print "someone clicked the button!"
myButton = Button(myClickFunction)
# after some time.... someone clicks the button.
# it displays "someone clicked the button!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment