L5: Advanced Functions#

Overview#

This lecture explores advanced function concepts in Python, building on the fundamentals from L4. You will learn about programming paradigms (procedural, functional, object-oriented), first-class functions, lambda expressions, closures, callables, decorators, and partial functions. These concepts are essential for writing clean, reusable, and composable Python code and form the foundation for object-oriented programming in later lectures.

Learning Objectives

By the end of this lecture, you will be able to:

  • Distinguish imperative, functional, and object-oriented programming paradigms.

  • Use functions as first-class objects: assign, pass, and return them.

  • Write anonymous functions with lambda and understand their limitations.

  • Explain closures and how inner functions capture enclosing state.

  • Make objects callable with the __call__ dunder method.

  • Write and apply decorators to extend function behavior.

  • Stack multiple decorators and preserve function metadata with functools.wraps.

  • Create partial functions using functools.partial.

  • Use built-in higher-order functions: map, filter, and sorted with key.

Next Steps#

  • In the next lecture, we will cover Object-Oriented Programming I:

    • Classes and objects

    • Attributes and methods

    • Constructors and __init__

    • Encapsulation and properties

    • Dunder methods

  • Review and experiment with all code snippets and exercises from today’s lecture.

  • Practice writing decorators and closures.

  • Read Real Python: Python Classes.