Created
January 16, 2012 07:57
-
-
Save leewardbound/1619681 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
def curry_instance_attribute(attr, func_name, instance): | |
""" Curries the named attribute to the named function | |
>>> class Person(): | |
... def __init__(self, name): | |
... self.name = name | |
... curry_instance_attribute('name', 'print_record', self) | |
... @classmethod | |
... def print_record(cls, name): | |
... print 'Person',name | |
>>> Person.print_record('bob') | |
Person bob | |
>>> p=Person('jane') | |
>>> p.print_record() | |
Person jane | |
""" | |
from types import MethodType | |
func = getattr(instance, func_name) | |
def curried(self, *args, **kwargs): | |
return func(getattr(self, attr), | |
*args, **kwargs) | |
setattr(instance, func_name, | |
MethodType(curried, instance, instance.__class__)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment