Created
June 1, 2011 01:03
-
-
Save j0hn/1001582 to your computer and use it in GitHub Desktop.
How to add a method to an object's instance
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
#!/usr/bin/env python | |
# coding: utf-8 | |
""" | |
Shows how to add a method to a instance of an object. | |
""" | |
class Useless: | |
""" | |
Does nothing, unless you hack it. | |
""" | |
pass | |
def hacker_method(self): | |
""" | |
Returns hacked object. | |
""" | |
return self | |
my_useless = Useless() | |
my_useless.hacker_method = hacker_method.__get__(my_useless) | |
print my_useless == my_useless.hacker_method() # True | |
print id(my_useless) == id(my_useless.hacker_method()) # Also True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment