Complete worked example
python
def inspect_example(offsets, value):
functions = []
for offset in offsets:
def shifted(number, offset=offset):
return number + offset
functions.append(shifted)
return [function(value) for function in functions]- Reasoning
- For offsets one, three and five, the loop creates three callable objects. Each shifted function receives its own default value and the functions are invoked only after construction finishes. Calling them with ten returns eleven, thirteen and fifteen in the original order.