Function call with 3 or more argument input fields - function () () ()

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

Write a function that returns the strength of a number. Conditions: a function can take only one argument and must use another function to return the power value of a given number.

Code that solves this exercise:

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

For example, if we want to know the value of power: 2 ^ 3, we will call the function as follows: power (3) (2)

Here is what I would like to know:

Is there a 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 possible, could you give me an example of the code for this function and explain it briefly?

also:

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

    def power_another(z):
        return z

    return power_extra and power_another

?

+5
2

, :

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

2 (power_times(2)), , lambda x: lambda y: 2 * y ** x ( , , " 2" ).

lambda :

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

, , def :

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

, , lambda - :

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
+4

@ David . undefined __call__ __repr__ __int__ .

>>> 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 
+3

All Articles