Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code:</p> <pre><code>class Curried(object): def __init__(self, func, args=None, kwargs=None): self._func = func self._args = () if args is None else args[:] self._kwargs = {} if kwargs is None else dict(kwargs) self._name = None def __call__(self, *args, **kwargs): if args or kwargs: return Curried(self._func, self._args + args, dict(self._kwargs.items() + kwargs.items())) else: return self._func(*self._args, **self._kwargs) def __str__(self): if self._name is None: self._name = self._get_curried_name() return self._name def _get_curried_name(self): _args = ([str(a) for a in self._args] + ['{}={}'.format(k, v) for k, v in self._kwargs.iteritems()]) all_args = ", ".join(_args) return '&lt;curried {}({}) at 0x{:x}&gt;'.format( self._func.func_name, all_args, id(self)) def curry(func): _curried = Curried(func) return _curried </code></pre> <p>Test:</p> <pre><code>@curry def f(a, b, c, flag_foo=True, flag_bar=False): return 'horray!' print f print f(1, 2, flag_bar=True) print f(1, 2, flag_bar=True)(3, flag_foo=False) print f(1, 2, flag_bar=True)(3, flag_foo=False)() </code></pre> <p>And the result:</p> <pre><code>&lt;curried f() at 0x100484210&gt; &lt;curried f(1, 2, flag_bar=True) at 0x100484250&gt; &lt;curried f(1, 2, 3, flag_bar=True, flag_foo=False) at 0x100484310&gt; horray! </code></pre> <p>Instead of a "pure" function, here you get a callable class that acts as a function. You evaluate the name only once. Of course, if you want <code>func_name</code> you can add such variable as <a href="http://docs.python.org/2/library/functions.html#property" rel="nofollow"><code>property</code></a> and return <code>str(self)</code> which is evaluated once</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload