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?
() (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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With