Skip to content

Instantly share code, notes, and snippets.

@richo
Created March 13, 2013 02:35
Show Gist options
  • Save richo/5148950 to your computer and use it in GitHub Desktop.
Save richo/5148950 to your computer and use it in GitHub Desktop.
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