Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Calling 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 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?

like image 962
Testing360 Avatar asked Jan 23 '13 16:01

Testing360


People also ask

How can you accept multiple arguments into a function?

Some functions are designed to return values, while others are designed for other purposes. We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times. Example: Python.

How many arguments can a function call have?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

What will happen if you call any function with more or less arguments than required in Python?

Passing many arguments will have no effect to the function, as long as the parameters are filled in.

What are the three types of function arguments?

5 Types of Arguments in Python Function Definition:keyword arguments. positional arguments. arbitrary positional arguments.


2 Answers

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
like image 185
David Robinson Avatar answered Nov 14 '22 22:11

David Robinson


@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 
like image 34
Abhijit Avatar answered Nov 14 '22 22:11

Abhijit