PNL Learn

Python Functions in Depth

Defaults, scope, *args, lambdas, closures and decorators.

Functions are where Python programs get their shape. You will match arguments by position and keyword and read the TypeErrors when matching fails, give parameters defaults (and dodge the mutable default trap), return early with guard clauses and catch the paths that fall off the end into None. Then flexible signatures with *args, **kwargs and keyword-only parameters, scope and the LEGB rule (UnboundLocalError, global, nonlocal, shadowed built-ins), functions as values with lambda, key=, map, filter and your first generators, closures that keep private state, decorators with and without arguments, and recursion with base cases. The course ends by building a discount pipeline, a memoize decorator and an event dispatcher from small functions.

IntermediatePython6 lessons71 exercisesAbout 5 h
Create a free account

What you will learn

  • Default and keyword arguments
  • Guard clauses and return values
  • *args and **kwargs
  • Scope and LEGB
  • Lambdas and key functions
  • map, filter and generators
  • Closures
  • Decorators
  • Recursion

Lessons

  1. 1

    Parameters and Arguments

    Call functions by position or by name, give parameters defaults, return early on purpose, and dodge the mutable default trap.

    11 exercises

  2. 2

    Flexible Signatures

    Accept any number of arguments with *args and **kwargs, force keywords, and unpack at the call site.

    12 exercises

  3. 3

    Scope

    Know which variable a name refers to, avoid UnboundLocalError and shadowed built-ins, and prefer return values over globals.

    12 exercises

  4. 4

    Functions Are Values

    Pass functions around, return them, write lambdas, sort, filter and map with them, and write a generator with yield.

    14 exercises

  5. 5

    Closures and Decorators

    Keep private state inside a function with closures, then wrap functions with decorators that clamp, count, retry and guard.

    12 exercises

  6. 6

    Recursion and Putting It Together

    Solve nested problems with recursion and a base case, then build a discount pipeline, a memoize decorator and an event dispatcher.

    10 exercises

How you practice

You practice in the browser and every exercise gives you feedback right away. This course uses these formats:

  • Code exercise: 27
  • Fix the bug: 14
  • Predict the output: 8
  • Multiple choice: 7
  • Fill in the blank: 7
  • Type the answer: 4
  • Match the pairs: 1
  • Spot the bug: 1
  • Select all that apply: 1
  • Reorder lines: 1

Aligned to

PCEP-30-02Python InstituteSource
  • Functions and Exceptions
    • 4.1Decompose the code using functions
    • 4.2Organize interaction between the function and its environment
PCAP-31-03Python InstituteSource
  • Miscellaneous
    • 5.2Embed lambda functions into code
    • 5.3Define and use closures
CS2023ACM / IEEE-CS / AAAISource
  • Software Development Fundamentals
    • SDF-FundamentalsFundamental Programming Concepts and Practices

Proto Node Labs is not affiliated with or endorsed by Python Institute and ACM / IEEE-CS / AAAI. Exam names are trademarks of their owners.