1

Let's say I have the following 2 functions:

def add(*numbers, squared=False):
   return (sum(numbers) ** 2) if squared else sum(numbers)
def multiply(*numbers, half=False):
    mult = 1
    for i in numbers:
        mult *= i
    return (mult / 2) if half else mult

What I want to do is assign a variable (multiply_add) so that I can use it as a function and pass parameters to it but in reality, it's just the multiply function that takes the return of the add function as the first parameter but I'm the one who actually gives the numbers for the add function as well.

So basically something like this:

multiply_add = multiply(add(NUMBERS))

# And I can call it like so:
multiply_add((5, 3, 2), 7, half=True)

# In this case the result would be 35.0 (add = 10, then 10*7 = 70 / 2 = 35)

The above is just an example (and not even a very good one) but I think the idea is clear. Please don't suggest anything that's only applicable to the given code but not the idea itself.

I tried using the partial function from the functools library but I couldn't figure out how to use it so I can achieve what I want.

I'm aware that I can just write a new function and deal with all that but I'm asking if there's a simpler method like using the partial function or something else I can't think of at the moment.

I'm very sorry for this question, I'm sure there's already an answer to it somewhere but I didn't know what to search for and so couldn't find an answer.

3
  • 1
    Why not just write a new function? You could use a lambda expression like multiply_add = lambda numbers: multiply(add(numbers)), but this is generally only used in an anonymous context. You don't want partial because that's for pre-defining some of the arguments to a function, not pre-defining some of the implementation of it. Commented Feb 8, 2023 at 17:17
  • This kind of Boolean argument isn't a good practice. While you're deciding whether to pass True or False to add, you could just as easily be deciding whether to call add or add_squared instead. In general, you could accept an arbitrary pre-processing function (default lambda x: x) to apply to each of the numbers before summing the results. Commented Feb 8, 2023 at 17:45
  • To the first comment - yeah, I also thought I could use a simple lambda but was curious if there's a better way to do it. As for the second comment - I know that about the bool but as I've stated in the question, these are just some example functions. In reality both functions are a lot more complex and for the inner one (add in this case) I want to have all arguments pre-defined with the exception of the ones in *args (in this case the numbers) Commented Feb 8, 2023 at 20:39

1 Answer 1

1

it seems what you are looking for is lambda. Using it you can do

multiply_add = lambda a, b, half: multiply(add(*a), b, half=half)
multiply_add((5, 3, 2), 7, half=True)  # will return 35

type hinting lambda can be done by importing Callable from the typing module and using it like this:

multiply_add: Callable[[tuple[int], int, bool], int | float] = lambda a, b, half: multiply(add(*a), b, half=half)
Sign up to request clarification or add additional context in comments.

2 Comments

I already thought of the lambda version but somehow thought there might be a better way to do this. I'll wait for a few days to see if something shows up and will probably accept this answer if nothing better appears. Also, It just came to me that neither by using partial nor lambda will I have the chance to have a proper docstring for the new function which is really important to me in this case. I know I could use the ._doc._ for that but it wouldn't work with mouse hover, only with the help function.. In any case, I guess the best choice would really be to use a normal function.
@AleksK. I recommend editing your question to include the fact that you need a doc string. maybe there is a way but not that i would be aware

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.