Skip to content

Instantly share code, notes, and snippets.

@j0hn
Created June 1, 2011 01:03
Show Gist options
  • Save j0hn/1001582 to your computer and use it in GitHub Desktop.
Save j0hn/1001582 to your computer and use it in GitHub Desktop.
How to add a method to an object's instance
#!/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