[Python] curry?

Rob Hudson rob at euglug.net
Thu Oct 5 14:30:16 PDT 2006


I just realized this method is in django.utils.functional.  Here's the 
method in full:

def curry(_curried_func, *args, **kwargs):
     def _curried(*moreargs, **morekwargs):
         return _curried_func(*(args+moreargs), **dict(kwargs, 
**morekwargs))
     return _curried

I think, in general, there's a concept about "curry" or "currying" that 
I'm not familiar with.

-Rob

Rob Hudson wrote:
> What's curry?
> 
> I read a short article on it but didn't "get it".  I think I understand 
> the basic concept but am failing to find a use case for it.  Seeing 
> examples of the application helps me understand things better.
> 
> Here's an example in Django that I'm looking at:
> 
>     def inclusion_tag(self, file_name, context_class=Context, 
> takes_context=False):
>         def dec(func):
>             params, xx, xxx, defaults = getargspec(func)
>             if takes_context:
>                 if params[0] == 'context':
>                     params = params[1:]
>                 else:
>                     raise TemplateSyntaxError, "Any tag function 
> decorated with takes_context=True must have a first argument of 'context'"
> 
>             class InclusionNode(Node):
>                 def __init__(self, vars_to_resolve):
>                     self.vars_to_resolve = vars_to_resolve
> 
>                 def render(self, context):
>                     resolved_vars = [resolve_variable(var, context) for 
> var in self.vars_to_resolve]
>                     if takes_context:
>                         args = [context] + resolved_vars
>                     else:
>                         args = resolved_vars
> 
>                     dict = func(*args)
> 
>                     if not getattr(self, 'nodelist', False):
>                         from django.template.loader import get_template
>                         t = get_template(file_name)
>                         self.nodelist = t.nodelist
>                     return self.nodelist.render(context_class(dict))
> 
>             compile_func = curry(generic_tag_compiler, params, defaults, 
> func.__name__, InclusionNode)
>             compile_func.__doc__ = func.__doc__
>             self.tag(func.__name__, compile_func)
>             return func
>         return dec
> 
> Thanks,
> Rob
> 
> _______________________________________________
> Python mailing list
> Python at euglug.org
> http://www.euglug.org/mailman/listinfo/python
> 


More information about the Python mailing list