object()()
is just _result = object()
and _result()
chained. As long as the first call returns a callable object, the second call expression can proceed.
Have your examplefunction()
call return another function. Functions are objects too, so you could just return an existing function:
def examplefunction():
return secondfunction
def secondfunction():
return 10
at which point examplefunction()()
produces 10
.
You can also have your examplefunction()
call produce a new function object each time, by defining that function object as part of its body:
def examplefunction():
def nestedfunction():
return 10
return nestedfunction
This gives the same result, but the added advantage that nestedfunction()
has (read) access to all local names defined in examplefunction
(these are called closures); this allows you to parameterise how nestedfunction()
behaves:
def examplefunction(base):
def nestedfunction():
return base * 2
return nestedfunction
examplefunction(5)() # produces 10
I suspect that your assignment is trying to teach you these principles.