Examveda

What is the order of namespaces in which Python looks for an identifier?

A. Python first searches the built-in namespace, then the global namespace and finally the local namespace

B. Python first searches the built-in namespace, then the local namespace and finally the global namespace

C. Python first searches the local namespace, then the global namespace and finally the built-in namespace

D. Python first searches the global namespace, then the local namespace and finally the built-in namespace

Answer: Option B

Solution (By Examveda Team)

When Python encounters an identifier (variable or function name), it searches for it in a specific order across different namespaces.
The order of namespaces that Python looks for an identifier is:
Built-in namespace: Python first searches the built-in namespace, which contains the names of all built-in functions, exceptions, and constants.
Local namespace: Next, Python searches the local namespace, which contains names defined within the current function or code block.
Global namespace: If the identifier is not found in the local namespace, Python searches the global namespace, which contains names defined at the top level of the current module or in the global scope.
If the identifier is not found in any of these namespaces, Python raises a NameError.
Therefore, the correct order of namespaces in which Python looks for an identifier is: built-in namespace, local namespace, global namespace.

This Question Belongs to Python Program >> Introduction To Python

Join The Discussion

Related Questions on Introduction to Python