Welcome

sampleqa.in tutorials, Python Programming  tutorial,Python Variable Scope Basics

Python Variable Scope Basics


     Variables declared and defined in the Program , has scope associated with in. Namespace is a place variables have reference in it. Variables can be declared outside the function,variables as function parameters, variables declared with in the function,variables declared with in the nested functions, variables in the class attributes.

Variables has following scope
  • Local Scope
  • Nested Scope
  • Global Scope
  • built-in scope

Note:

A class is never used as a global scope

LEGB Rule

  1. L: Local Scope
  2.      Variables declared at Function level

  3. E: Enclosed Scope
  4.      This technique is used when nested functions looks for Variables

  5. G: Global Scope
  6.      Variables declared at Module level

  7. B: Built-in Scope
  8.      Python Framework Variables and methods in builtins module are available to run Python code without any external imports