5

I am studying the properties of functions in Python and I came across an exercise that asks to:

Write a function which returns de power of a number. Conditions: The function may only take 1 argument and must use another function to return the value of the power of a given number.

The code that solves this exercise is:

def power(x):
    return lambda y: y**x

For example, if we would like to know the value of the power: 2^3, we would call the function like this: power(3)(2)

Here is what I would like to know:

Is there any way to write a function that, when called, has a similar structure: function()()(). In other words, is it possible to write a function, that requires three or more parentheses ()()() when called? If it is possible, could you please give me an example code of that function and briefly explain it?

Also:

def power(x):
    def power_extra(y):
        return y

    def power_another(z):
        return z

    return power_extra and power_another

Possible?

1
  • For your information, this is called currying. Commented Dec 4, 2013 at 16:29

2 Answers 2

4

Sure you can:

def power_times(k):
    """use as power_times(k)(x)(y) => k * y^x"""
    return lambda x: lambda y: k * y**x

print power_times(2)(3)(4)  # returns 2 * 4^3 = 128

When you call this function with argument 2 (power_times(2)), it returns a lambda function that works like lambda x: lambda y: 2 * y ** x (that is, like your original function, only with an extra "times 2").

You can stack as many lambdas on top of each other as you like:

def many_lambdas(x):
    """many_lambdas(x)(y)(z)(q) => x + y * z^q"""
    return lambda y: lambda z: lambda q: x + y * z ** q

print many_lambdas(1)(2)(3)(4) # prints 163

Indeed, it might be even clearer if you skipped using def at all, and just wrote:

many_lambdas = lambda x: lambda y: lambda z: lambda q: x + y * z ** q

Or, alternatively, you could skip using lambda ever and just use them as nested functions:

def many_funcs(x):
    def many_funcs_y(y):
        def many_funcs_z(z):
            def many_funcs_q(q):
                return x + y * z ** q
            return many_funcs_q
        return many_funcs_z
    return many_funcs_y

print many_funcs(1)(2)(3)(4)  # prints 163
Sign up to request clarification or add additional context in comments.

4 Comments

Would it be possible to have two functions nested inside the top level function and provide the arguments with the structure: function(<argument>)(<argument for nested function 1>, <argument for nested function 2>) or in any other way?
@JoãoCastro: You wouldn't need to nest two functions to do that, you could just change the body of the function to return lambda x, y: k * y**x and it would work as power_times(2)(3, 4) (returning 2 * 4^3). Or did I miss your point?
I meant as if I had two functions with names (instead of lambdas) inside the function. ^^
If you are giving both of the arguments to the top level function, why would you have it as two nested functions? (If you want, you could edit an example of what you're looking for into your original question).
3

@David's answer would aptly answer you question for fixed nested function calls. For undefined nesting, you may want to define a class and overload the __call__ method along with __repr__ and __int__ to serve your Purpose.

>>> class Power(object):
    def __init__(self, value):
        self.value = value
    def __call__(self, value):
        self.value **= value
        return self
    def __int__(self):
        return self.value
    def __repr__(self):
        return str(self.value)


>>> print Power(2)(2)(2)(2)(2)
65536
>>> int(Power(2)(2)(2)(2)(2)) / 2
32768 

Comments

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.