+1 vote
in Class 12 by kratos

Explain Namespaces and Scoping.

1 Answer

+4 votes
by kratos
 
Best answer

Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corres-ponding objects (values).

A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable. Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions. Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local.

Therefore, in order to assign a value to a global variable within a function, you must first use the global statement.The statement global VarName tells Python that VarName is a global variable.

Python stops searching the local namespace for the variable.

...