Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the parenthesis after the function mean

I was asked to write a function in Python 3 for: Write a function called general_poly, that would, for example, evaluate general_poly([1, 2, 3, 4])(10) to 1234 because 1*10^3 + 2*10^2 + 3*10^1 + 4*10^0.

Now I don't understand what the second parenthesis, (10), means.

How would my function general_poly know, to take that value inside itself and use it?

like image 485
ccasimiro9444 Avatar asked May 04 '18 08:05

ccasimiro9444


People also ask

What are the parentheses after a function for?

() (parentheses) They are used to contain a list of parameters passed to functions and control structures and they are used to group expressions to control the order of execution. Some functions have no parameters and in this case, the space between parentheses is blank.

What is () after function in JavaScript?

Show activity on this post. (function () {}) creates an anonymous function. Adding the () to the end calls the function that was just created. In the case of this particular function, the anonymous function returns several properties to the Browser object.

What goes inside the parentheses of function definition?

A function call consists of the function followed by its arguments (if any) inside parentheses, separated by comments. For a function with no arguments, call it with nothing between the parentheses. A function call that returns a value can be used in an expression just like a variable.

What does function with brackets mean?

Basically this lets you declare an anonymous function, and then by enclosing it in parentheses and writing (someWord) you are running the function. You could think of it as declaring an object and then immediately instantiating the object.


2 Answers

It means that your function should return a function:

def multiplier(times):
    def inner(num):
         return num * times
    return inner

result = multiplier(2)(3)
print(result)
# 6

times_four = multiplier(4)
print(times_four(3))
# 12

This may or may not be an overkill, but if you need to save state you can even use a class and implement __call__:

class multiplier:
    def __init__(self, times):
        self.times = times
    def __call__(self, num):
        return num * self.times

print(multiplier(2)(3))
# 6
like image 101
DeepSpace Avatar answered Nov 15 '22 08:11

DeepSpace


A function is a first-class object in Python. One of the corollaries of this is that a function may return another function.

Your particular example can be written as follows:

def general_poly(A):
    k = len(A)
    def power(n):
        return sum(j*n**(k-i) for i, j in enumerate(A, 1))
    return power

res = general_poly([1, 2, 3, 4])(10)  # 1234

The outer function general_poly returns the inner function power. The former takes a list, or an array, while the latter takes a scalar.


An alternative way of structuring your logic is to use functools.partial. This creates a new function which one or more parameters fixed and obviates the need for an inner function.

from functools import partial

def general_poly(A, n):
    k = len(A)
    return sum(j*10**(k-i) for i, j in enumerate(A, 1))

genral_poly_10 = partial(general_poly, n=10)
res = genral_poly_10([1, 2, 3, 4])  # 1234

Of course, this is only useful if you wish to create a function for a specific power; trivially, n can be used directly as an additional parameter without functools.partial.


Note: Beware of mutable default arguments. So if your function has a mutable default argument, e.g. setting A=[] in the above examples, the same list may be reused in an unforeseen way.

like image 42
jpp Avatar answered Nov 15 '22 07:11

jpp