Practice mode Python

In what order does Python search scopes when resolving a name (the LEGB rule)?

Correct answer B. Local, Enclosing, Global, Built-in

Explanation

Python first looks in the local scope of the current function, then in the scopes of any enclosing functions, then in the module's global scope, and finally in the built-in namespace. This is why a local variable can shadow a built-in such as len.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.