locals()
A built-in function that returns a dictionary of the current local variable names and their values.
In more detail
The course uses it with a format string, template.format(**locals()), to fill named fields from variables of the same names. It is handy but hides which variables a template needs, so f-strings are usually clearer today.
Related terms
- Python namespace — A mapping from names to objects; each module, function call and class has its own, which is why the same name can mean different things in different places.
- dictionary unpacking — Writing ** before a dictionary in a function call to pass each of its key-value pairs as a keyword argument.
- format method — The string method that returns a copy of a string with each {} replacement field filled in from its arguments.
Taught in
- Dictionaries And String Formatting — Hands-on Python: Complete Video Course & Book
- Exponents, Roots And Float Formats — Hands-on Python: Complete Video Course & Book
- Chapter One In One Sitting — Hands-on Python: Complete Video Course & Book
- Composing Web Pages In Python — Hands-on Python: Complete Video Course & Book