Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:41:24+00:00 2026-06-14T17:41:24+00:00

So I was playing around with currying functions in Python and one of the

  • 0

So I was playing around with currying functions in Python and one of the things that I noticed was that functools.partial returns a partial object rather than an actual function. One of the things that annoyed me about this was that if I did something along the lines of:

five = partial(len, 'hello')
five('something')

then we get

TypeError: len() takes exactly 1 argument (2 given)

but what I want to happen is

TypeError: five() takes no arguments (1 given)

Is there a clean way to make it work like this? I wrote a workaround, but it’s too hacky for my taste (doesn’t work yet for functions with varargs):

def mypartial(f, *args):
  argcount = f.func_code.co_argcount - len(args)
  params = ''.join('a' + str(i) + ',' for i in xrange(argcount))
  code = '''
def func(f, args):
  def %s(%s):
    return f(*(args+(%s)))
  return %s
  ''' % (f.func_name, params, params, f.func_name)

  exec code in locals()
  return func(f, args)

Edit: I think it might be helpful if I added more context. I’m writing a decorator that will automatically curry a function like so:

@curry
def add(a, b, c):
  return a + b + c

f = add(1, 2) # f is a function
assert f(5) == 8

I want to hide the fact that f was created from a partial (maybe a bad idea :P). The message that the TypeError message above gives is one example of where whether something is a partial can be revealed. I want to change that.

This needs to be generalizable so EnricoGiampieri’s and mgilson’s suggestions only work in that specific case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:41:25+00:00Added an answer on June 14, 2026 at 5:41 pm

    You definitely don’t want to do this with exec.

    You can find recipes for partial in pure Python, such as this one—many of them are mislabeled as curry recipes, so look for that as well. At any rate, these will show you the proper way to do it without exec, and you can just pick one and modify it to do what you want.

    Or you could just wrap partial…

    However, whatever you do, there’s no way the wrapper can know that it’s defining a function named “five”; that’s just the name of the variable you store the function in. So if you want a custom name, you’ll have to pass it in to the function:

    five = my_partial('five', len, 'hello')
    

    At that point, you have to wonder why this is any better than just defining a new function.

    However, I don’t think this is what you actually want anyway. Your ultimate goal is to define a @curry decorator that creates a curried version of the decorated function, with the same name (and docstring, arg list, etc.) as the decorated function. The whole idea of replacing the name of the intermediate partial is a red herring; use functools.wraps properly inside your curry function, and it won’t matter how you define the curried function, it’ll preserve the name of the original.

    In some cases, functools.wraps doesn’t work. And in fact, this may be one of those times—you need to modify the arg list, for example, so curry(len) can take either 0 or 1 parameter instead of requiring 1 parameter, right? See update_wrapper, and the (very simple) source code for wraps and update_wrapper to see how the basics work, and build from there.

    Expanding on the previous: To curry a function, you pretty much have to return something that takes (*args) or (*args, **kw) and parse the args explicitly, and possibly raise TypeError and other appropriate exceptions explicitly. Why? Well, if foo takes 3 params, curry(foo) takes 0, 1, 2, or 3 params, and if given 0-2 params it returns a function that takes 0 through n-1 params.

    The reason you might want **kw is that it allows callers to specify params by name—although then it gets much more complicated to check when you’re done accumulating arguments, and arguably this is an odd thing to do with currying—it may be better to first bind the named params with partial, then curry the result and pass in all remaining params in curried style…

    If foo has default-value or keyword args, it gets even more complicated, but even without those problems, you already need to deal with this problem.

    For example, let’s say you implement curry as a class that holds the function and all already-curried parameters as instance members. Then you’ll have something like this:

    def __call__(self, *args):
        if len(args) + len(self.curried_args) > self.fn.func_code.co_argcount:
            raise TypeError('%s() takes exactly %d arguments (%d given)' %
                            (self.fn.func_name, self.fn.func_code.co_argcount,
                             len(args) + len(self.curried_args)))
        self.curried_args += args
        if len(self.curried_args) == self.fn.func_code.co_argcount:
            return self.fn(*self.curried_args)
        else:
            return self
    

    This is horribly oversimplified, but it shows how to handle the basics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While playing around with the emulator, I noticed that when trying to view a
When playing around with multiprocessing I noticed that in the following script, __del__ is
Whilst playing around with an nhibernate mapping, I noticed that a property setter I
Playing around with my web application in the iOS 5 simulator, I noticed that
Playing around with MSVC++ 2005, I noticed that if the same class is defined
After playing around with Sass for the first time, I noticed that it leaves
Playing around with Python - tkInter - Entry widget - when I use validatecommand
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
Im playing around with TryParse() But lets say the parsing fails, then returns false,
playing around with a new samsung tablet i noticed they have mini apps on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.