- All Courses
- Python Foundations for Data Work
- Python Essentials
- Functions, arguments, and scope
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.
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
Notes are not saved yet — they clear when you leave this page.
Control flow and comprehensions
10m
Modules, packages, and virtual environments
9m