MCS-201 · Unit 11

Functions and File Handling in Python

Section 5 of 8

Block 3 · Unit 11Functions and File Handling in Python9 min · Section 5 of 8

11.5Functions as Objects

Covers IGNOU MCS-201 §11.6

In Python, a function is an object, just like a number, a string or a list. This simple fact makes powerful techniques possible: passing behaviour as an argument, building tables of operations, and functions that create other functions.

What you can do with a function object

AbilityExample
Assign to another namesay = greet, then say("Nav")
Store in a data structureops = [str.upper, str.lower, len]
Pass as an argumentapply(add, [1, 2, 3]), sorted(words, key=len)
Define inside another functiondef outer(): def inner(): ...
Return from a functionreturn inner (a closure)

Assigning creates a second name for the same function; deleting one name with del does not delete the function while another name still refers to it.

Nested functions and closures

A function defined inside another is a nested function. It can read the variables of the enclosing function. If the outer function returns the inner one, the inner function keeps access to those variables even after the outer function has finished; this combination is a closure.

Key takeaways

  • Functions are objects with names that can be rebound.
  • They can be stored in data structures and passed as arguments.
  • Nested functions see the enclosing function’s variables.
  • Returning an inner function creates a closure that remembers those variables.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

After say = greet, what does say refer to?

Show answer

Answer: B. The same function object as greet

Assignment binds another name to the same object; no call happens and nothing is copied.

Question 2 · multiple choice

Which call passes a function as an argument?

Show answer

Answer: C. sorted(words, key=len)

key=len passes the function len itself, which sorted calls on each item.

Question 3 · multiple choice

What does this program print?

Python · quiz_closure.py
def make_adder(n):
    def add(x):
        return x + n
    return add

plus5 = make_adder(5)
print(plus5(10), make_adder(1)(1))
Show answer

Answer: C. 15 2

plus5 remembers n = 5, so plus5(10) is 15; make_adder(1) returns a function that is immediately called with 1, giving 2.

Question 4 · multiple choice

What does this program print?

Python · quiz_funclist.py
ops = [min, max, sum]
print([f([4, 1, 7]) for f in ops])
Show answer

Answer: A. [1, 7, 12]

Each function in the list is called on the same input: min 1, max 7, sum 12.

Question 5 · multiple choice

What is a closure?

Show answer

Answer: D. An inner function that keeps access to variables of the function in which it was created

The returned inner function carries those values with it, even after the outer function has returned.

Question 6 · short answer

Explain, with an example, why it is useful to pass a function as an argument.

Show worked answer

Passing a function lets one general routine perform different operations without being rewritten. For example, def apply(func, values): return [func(v) for v in values] can square numbers with apply(lambda x: x * x, nums), take absolute values with apply(abs, nums) or round them with apply(round, nums). The loop is written once; the behaviour is chosen by the caller. Built-ins such as sorted(words, key=len) and map() use exactly this idea.

↑ ↓ move↵ openTab switch type