Functions & Scope · Question 14 · Medium

When Python looks up a name used inside a function, in which order does it search the scopes?

  1. Global, Local, Enclosing, Built-in
  2. Local, Enclosing, Global, Built-in
  3. Built-in, Global, Enclosing, Local
  4. Local, Global, Enclosing, Built-in
Answer and explanation

Answer: B. Local, Enclosing, Global, Built-in

Explanation: This is the LEGB rule: the function's own local scope first, then any enclosing function scopes, then the module's global scope, and finally the built-in names.

Previous: Two counters are built from the same closure factory. What does the last line… · Next: A function signature mixes normal parameters, *rest and a keyword-only…

Practise functions & scope

Try a fresh set of functions & scope questions with an explanation after every answer, free and without signing up: Functions & Scope practice. Or sit a full placement mock test.

More functions & scope questions

More python practice

Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge