Skip to content

Instantly share code, notes, and snippets.

@leewardbound
Created January 16, 2012 07:57
Show Gist options
  • Save leewardbound/1619681 to your computer and use it in GitHub Desktop.
Save leewardbound/1619681 to your computer and use it in GitHub Desktop.
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