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

  • Home
  • SEARCH
  • 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 8574455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:28:42+00:00 2026-06-11T19:28:42+00:00

Suppose I have a function which I have written as a binary-operator (binop), how

  • 0

Suppose I have a function which I have written as a binary-operator (binop), how do I extend it to a multi-operator (multiop) which takes an arbitrary number of arguments? Is there such a decorator in a library (e.g. in functools)?

For example (I want a decorator to give this behaviour):

@binop_to_multiop
def mult(a,b):
    return a*b

mult(2,3,4) # 2*3*4 = 24
mult(7) # 7
mult(2,3) # 6

Obviously I can’t ask a question about decorators without mentioning, this answer.

.

I’ve tried writing my own, but can’t quite get it working, any explanation of where I’m going wrong would also be welcome:

def binop_to_multiop(f):
    @functools.wraps(f)
    def wrapper(*args, **kwds):
        if len(args) == 1: return args[0] # fails
        return f(args[0],(f(*args[1:], **kwds)), **kwds) #recursion attempt fails
    return wrapper

Gives a TypeError: mult() takes exactly 2 arguments (N given) (for various N!=2).

  • 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-11T19:28:43+00:00Added an answer on June 11, 2026 at 7:28 pm

    Your attempt to code it yourself was very close to working. You just need to change the recursive step to recurse on wrapper rather passing all but one argument to f:

    def binop_to_multiop(f):
        @functools.wraps(f)
        def wrapper(*args, **kwds):
            if len(args) == 1: return args[0]
            return f(args[0], wrapper(*args[1:], **kwds), **kwds)
        return wrapper
    

    I didn’t have any problems with the base case, so I’m not sure what your comment #fails was about.

    You may also need to think about which end of the list you start solving from (that is, does your operator have left or right associativity). For operators like multiplication and addition it won’t matter since (a+b)+c = a+(b+c), but for other you may get strange results. For instance, subtraction may not work as you might expect:

    @binop_to_multiop
    def sub(a, b):
        return a - b
    

    With the decorator defined above, sub(a, b, c) will give a different result than a-b-c (it will do a-(b-c) instead of (a-b)-c). If you want them to behave the same way, you can redefine the decorator to be left associative (like most mathematical operators do in most computer languages) like this:

    def left_associative_binop_to_multiop(f):
        @functools.wraps(f)
        def wrapper(*args, **kwds):
            if len(args) == 1: return args[0]
            return f(wrapper(*args[:-1], **kwds), args[-1], **kwds)
        return wrapper
    

    A more sophisticated approach would be to make the associativity be a parameter to the decorator, but that gets tricky if you don’t want the parameter to be required.

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

Sidebar

Related Questions

Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have a function which can either take an iterable/iterator or a non-iterable
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
Suppose this situation : I have a dll which was written in standard c++,
Suppose I have a CMS application written in Node.js which persists data on a
Suppose I have written such a class (number of functions doesn't really matter, but
I have a function in java which is written with a recursive algorithm that
Suppose, I have a js file which looks like this: $(document).ready(function() { // first
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have this function which is supposed to set a certain time format to

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.