Lesson 1.3

Functions, arguments, and scope

Python resolves a name by looking in the local scope, then any enclosing function, then the module, then the built-ins.

15mBeginner28.3k students

Overview

A function should be honest about what it touches

Python resolves a name by looking in the local scope, then any enclosing function, then the module, then the built-ins. Assigning to a name anywhere in a function makes it local for the whole function, which is why reading a global before assigning it raises an error.

Keyword arguments with defaults make a call site self-documenting, and are the difference between a call you can read and four bare booleans in a row.

A function that both returns a value and mutates its argument is hard to reuse and harder to test. Pick one.

In this lesson you will:

  • Use positional, keyword, and default arguments
  • Understand local, enclosing, and global scope
  • Return values instead of mutating inputs

Resources

Previous Lesson
Next Lesson
Functions, arguments, and scope — Python Foundations for Data Work — Vertex