Created
June 16, 2012 16:16
-
-
Save brycethomas/2941813 to your computer and use it in GitHub Desktop.
Function pointers up to delegates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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