1.

Python Variable Scope Resolution

Answer»

Scope of a variable is the part of the code, where it can be accessed freely and used by the program.

The scopes in the above image are explained as follows:



  • Built-in: These are reserved names for Python built-in modules.


  • Global: These variables are defined at the highest level.


  • Enclosed: These variables are defined inside some enclosing functions.


  • Local: These variables are defined inside the functions or class and are local to them.

The rules used in Python to resolve scope for local and global variables are as follows:


  • Code in the global scope cannot use any local variables.

  • Code in a function’s local scope cannot use variables in any other local scope.

  • However, a local scope can access global variables.

  • We can use the same name for different variables if they are in different scopes.




Discussion

No Comment Found