Created
March 13, 2013 02:35
-
-
Save richo/5148950 to your computer and use it in GitHub Desktop.
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
import functools | |
class Foo(object): | |
def __init__(self): | |
self.__dict__["overridden_methods"] = {} | |
def __setattr__(self, key, value): | |
"""Blindly assume that if we're setting attrs it's with new methods""" | |
self.overridden_methods[key] = value | |
def __getattr__(self, key): | |
if key in self.overridden_methods: | |
def _(*args, **kwargs): | |
return self.overridden_methods[key](self, *args, **kwargs) | |
return _ | |
else: | |
return super(Foo,self).__getattr__(key) | |
foo = Foo() | |
def func(self, value): | |
print repr(self) | |
print value | |
foo.foo = func | |
foo.foo("bar") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment